How to remove specific data from ArrayList <Integer> in android

I have one list of arrays and contains some values, for example 2,3,4,5,6. now how to check if a value is present and want to delete that particular value. please help me do this. tnx in advance.

I tried,

ArrayList<Integer> Positions=new ArrayList<Integer>(); Positions.remove(6); 

but it shows an error.

+4
source share
2 answers

Positions.remove(6); remove the item from a specific position.

So, first you have to compare the element in arraylist using for the loop, and get the position of this element and call Positions.remove(that Item Position in ArrayList).

Try this code.

 ArrayList<Integer> positions = new ArrayList<Integer>(); positions.add(3); // add some sample values positions.add(6); // add some sample values positions.add(1); // add some sample values positions.add(2); // add some sample values positions.add(6); for(int i=0;i<positions.size();i++) { if(positions.get(i) == 6) { positions.remove(i); } } Log.i("========== After Remove ",":: "+positions.toString()); 

Result: I / ========== After removal (309): [3, 1, 2]

+6
source

Try the following:

 ArrayList<Integer> positions = new ArrayList<Integer>(); positions.add(3); // add some sample values positions.add(6); // add some sample values positions.add(1); // add some sample values positions.add(2); // add some sample values int index = positions.indexOf(6); // finds the index of the first occurrence of 6 if (index >= 0) { // if not found, index will be -1 positions.remove(index); // removes this occurrence } 
+5
source

Source: https://habr.com/ru/post/1437939/


All Articles