Advertisement
airton-junior

Arrays e List

Apr 20th, 2023 (edited)
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. String [] arrayOfWords = new String[]{'mesa', 'cadeira', 'sofá'};
  2. System.debug(arrayOfWords[0]);
  3. //mesa
  4.  
  5. Integer [] numeros = new Integer[3];
  6. numeros[0] = 32;
  7. numeros[1] = 25;
  8. numeros[2] = 37;
  9.  
  10. System.debug(numeros[2]);
  11. //37
  12.  
  13. Integer [] numeros = new Integer[3];
  14. numeros[0] = 32;
  15. numeros[1] = 25;
  16. numeros[2] = 37;
  17.  
  18. System.debug(numeros[2]);
  19.  
  20. //**********************************************************************
  21. Account [] contas = new Account[3];
  22.  
  23. contas[0] = new Account(Name='Petrobrás');
  24. contas[1] = new Account(Name='IBM');
  25. contas[2] = new Account(Name='IBM');
  26.  
  27. System.debug(contas.size());
  28. System.debug(contas);
  29. contas.remove(2);
  30. System.debug(contas.size());
  31. System.debug(contas);
  32.  
  33. //**********************************************************************
  34.  
  35. List<String> palavras = new List<String>{'Antônio', 'José'};
  36. System.debug(palavras.size());//2
  37. palavras.add('Carlos');
  38. palavras.add('Kátia');
  39. System.debug(palavras.size());//4
  40. System.debug(palavras);//(Antônio, José, Carlos, Kátia)
  41. //**********************************************************************
  42.  
  43. List<String> palavras = new List<String>();
  44. System.debug(palavras.size());//0
  45. palavras.add('Carlos');
  46. palavras.add('Kátia');
  47. System.debug(palavras.size());//2
  48. System.debug(palavras);//(Carlos, Kátia)
  49. //**********************************************************************
  50.  
  51. List<SObject> sObjs = new List<sObject>();
  52. System.debug(sObjs.size());//0
  53. sObjs.add(new Contact(lastname='Pedro'));
  54. sObjs.add(new Account(Name='IBM'));
  55. System.debug(sObjs.size());//2
  56. System.debug(sObjs);//(contato, conta)
  57. System.debug(sObjs.get(1));//Conta
  58. System.debug(sObjs[1]);//Conta
  59. sObjs.add(new Account(Name='IBM'));
  60. sObjs.add(new Account(Name='IBM'));
  61. System.debug(sObjs.size());
  62. System.debug(sObjs);
  63. //**********************************************************************
  64.  
  65. List<SObject> sObjs = new List<sObject>();
  66. System.debug(sObjs.size());//0
  67. Contact cont1 = new Contact(lastname='Pedro');
  68. sObjs.add(cont1);
  69. Account acc1 = new Account(Name='IBM');
  70. sObjs.add(acc1);
  71. System.debug(sObjs.size());//2
  72. System.debug(sObjs);//(contato, conta)
  73. System.debug(sObjs.get(1));//Conta
  74. System.debug(sObjs[1]);//Conta
  75. Account acc2 = new Account(Name='IBM');
  76. sObjs.add(acc2);
  77. Account acc3 = new Account(Name='IBM');
  78. sObjs.add(acc3);
  79. System.debug(sObjs.size());
  80. System.debug(sObjs);
  81. //**********************************************************************
  82.  
  83. class Cachorro{
  84.     String pelo = 'liso';
  85.     String cor = 'preto';
  86.     public Cachorro(){
  87.        
  88.     }
  89.     public Cachorro(String pelo, String cor){
  90.         pelo = pelo;
  91.         cor = cor;
  92.     }
  93. }
  94. List<Object> objs = new List<Object>();
  95. objs.add(new Account(Name='IBM'));
  96. objs.add('Airton');
  97. objs.add(new Cachorro('liso', 'branco'));
  98. Cachorro c = new Cachorro('liso', 'preto');
  99. System.debug(c.cor);
  100. objs.add(c);
  101. objs.add(15);
  102. System.debug(objs.size());
  103. System.debug(objs);
  104.  
  105. System.debug('Objeto no índice 0 da lista: '+objs[0]); //Conta
  106. System.debug('Objeto no índice 2 da lista: '+objs[2]); //Cachorro
  107. System.debug('Objeto no índice 4 da lista: '+objs[4]); //Integer
  108.  
  109. //Quero pegar a cor do pelo do 2º cachorro
  110. Cachorro d = new Cachorro();
  111. d = (Cachorro)objs[3];
  112. System.debug(d.cor);
  113. System.debug(((Account)objs[0]).Name);
  114. System.debug(((SObject)objs[0]).get('Name')); //Aqui o get funciona por se
  115. //tratar de um sObject
  116.  
  117.  
  118.  
  119.  
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement