The idea is in a pile or arraylist to eliminate a data that is in the middle and that space that is left there is not null so that if several numbers are deleted there are no null spaces between the data of the array or stack.
The idea is in a pile or arraylist to eliminate a data that is in the middle and that space that is left there is not null so that if several numbers are deleted there are no null spaces between the data of the array or stack.
You use the method remove(int index)
of ArrayList
, here an example
ArrayList<String> letras = new ArrayList();
letras.add("a"); //index 0
letras.add("b"); //index 1
letras.add("c"); //index 2
letras.add("d"); //index 3
int index = 2;
letras.remove(index);
The new arrangement would be
"a", "b", "d"
Where the letter "d"
occupies the index 2
.
In an ArrayList with the remove()
method you achieve it, already in an Array it is a bit more complete. The remove()
method receives as a parameter the position of the ArrayList to be deleted. The remove()
method removes the position.
ArrayList<String> frutas = new ArrayList<String>();
frutas.add("mango");
frutas.add("pera");
frutas.add("manzana");
frutas.add("uva");
// El ArrayList posee 4 posiciones
frutas.remove(1); // posicion 2 eliminada (pera).
// El ArrayList ahora posee 3 posiciones.