How to eliminate in a pilo or arraylist a data and that the positions move? [closed]

0

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.

    
asked by Juan D Villamil 01.09.2017 в 03:42
source

2 answers

1

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 .

    
answered by 01.09.2017 в 03:53
-1

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.
    
answered by 01.09.2017 в 04:00