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 com método leftShift ou operador << | |
List<Integer> lista1 = [1].leftShift(2).leftShift(3).leftShift(4) // Lista resultante [1, 2, 3, 4] | |
List<Integer> lista2 = [1] << 2 << 3 << 4 // Lista resultante [1, 2, 3, 4] | |
List<Integer> lista3 = [] << 1 << 2 << 3 << 4 // Lista resultante [1, 2, 3, 4] | |
List<Integer> lista4 = [] | |
lista4 << 1 << 2 << 3 << 4 // Lista resultante [1, 2, 3, 4] | |
//##### 2. Concatenando listas com método plus ou operador + | |
List<Integer> listaPlus1 = [1, 2, 3].plus([4, 5, 6]).plus([7, 8]) // Lista resultante [1, 2, 3, 4, 5, 6, 7, 8] | |
List<Integer> listaPlus2 = [1, 2, 3] + [4, 5, 6] + [7, 8] // Lista resultante [1, 2, 3, 4, 5, 6, 7, 8] |
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. Removendo elemento pela posição/índice | |
List<Integer> lista = new ArrayList<>(List.of(1, 2)) | |
lista.removeAt(1) // retorna 2. Lista resultante [1] | |
lista.removeAt(0) // retorna 1. Lista resultante vazia [] | |
//#### 2. Removento elemento com método removeElement | |
List<Integer> lista2 = new ArrayList<>(List.of(1, 2)) | |
lista2.removeElement(1) // retorna true. Lista resultante [2] | |
lista2.removeElement(0) // retorna false. Lista resultante [2] | |
lista2.removeElement(2) // retorna true. Lista resultante vazia [] |
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. Removendo elemento pela posição/índice | |
List<Integer> lista = new ArrayList<>(List.of(1, 2)); | |
lista.remove(1); // retorna 2. Lista resultante [1] | |
lista.remove(0); // retorna 1. Lista resultante vazia [] | |
//##### 2. Removendo elemento | |
List<Integer> lista2 = new ArrayList<>(List.of(1, 2)); | |
lista2.remove((Integer) 1); // retorna true. Lista resultante [2] | |
lista2.remove((Integer) 0); // retorna false. Lista resultante [2] | |
lista2.remove((Integer) 2); // retorna true. Lista resultante vazia [] |
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] |
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
//#### Criando uma lista vazia | |
List<Integer> lista = new ArrayList<>(); | |
//#### Criando uma lista inicializada antes do java9 | |
List<Integer> lista = new ArrayList<>(Arrays.asList(1, 2)); | |
//#### Criando uma lista inicializada a partir do java9 | |
List<Integer> lista = new ArrayList<>(List.of(1, 2)); | |
// Em ambos os exemplos com inicialização a lista resultante será: [1, 2] |