Last active
April 26, 2022 18:02
-
-
Save marcusvoltolim/2765f1b56643a85574aee0a12f787e65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #### 1. Adicionando elemento ao final da lista | |
List<Integer> lista1 = new ArrayList<>(); | |
lista1.add(1); // Lista resultante [1] | |
lista1.add(2); // Lista resultante [1, 2] | |
// #### 2. Adicionando elemento numa posição | |
List<Integer> lista2 = new ArrayList<>(); | |
lista2.add(0, 1); // lista resultante [1] | |
lista2.add(0, 2); // lista resultante [2, 1] | |
lista2.add(1, 3); // lista resultante [2, 3, 1] | |
// #### 3. Setando/Substituindo elemento | |
List<Integer> lista3 = new ArrayList<>(List.of(1, 2)); | |
lista3.set(0, 4); // lista resultante [4, 2] | |
lista3.set(1, 5); // lista resultante [4, 5] | |
// #### 4. Adicionando vários elementos | |
List<Integer> lista5 = new ArrayList<>(List.of(1, 2, 3)); | |
lista5.addAll(List.of(4, 5, 6)); //retorna true. Lista resultante [1, 2, 3, 4, 5, 6] | |
// #### 5. Adicionando vários elementos à partir de uma posição | |
List<Integer> lista5 = new ArrayList<>(List.of(1, 2, 3)); | |
lista5.addAll(1, List.of(4, 5, 6)); //retorna true. Lista resultante [1, 4, 5, 6, 2, 3] | |
//##### Exceções | |
List<Integer> lista6 = new ArrayList<>(); | |
lista6.add(1, 1); // Indice inválido lança exceção: Index: 1, Size: 0 | |
lista6.set(0, 1); // Indice inválido lança exceção: Index 0 out of bounds for length 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment