Remove item from ArrayList

I have an ArrayList suppose list , and it has 8 AH elements, and now I want to remove the 1,3,5 position. An element stored in an int array from list , how can I do this.

I am trying to do this with

 ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); list.add("F"); list.add("G"); list.add("H"); int i[] = {1,3,5}; for (int j = 0; j < i.length; j++) { list.remove(i[j]); } 

But after the first element deleted by the position of the array is changed, and in the next iteration it will delete the wrong element or provide an exception.

+72
java arraylist
May 23 '12 at 5:56 a.m.
source share
10 answers

In this particular case, you must delete the items in descending order. The first index is 5 , then 3 , then 1 . This will remove items from the list without unwanted side effects.

 for (int j = i.length-1; j >= 0; j--) { list.remove(i[j]); } 
+100
May 23 '12 at 5:58 a.m.
source share

You can remove items from ArrayList using ListIterator ,

 ListIterator listIterator = List_Of_Array.listIterator(); /* Use void remove() method of ListIterator to remove an element from List. It removes the last element returned by next or previous methods. */ listIterator.next(); //remove element returned by last next method listIterator.remove();//remove element at 1st position listIterator.next(); listIterator.next(); listIterator.remove();//remove element at 3rd position listIterator.next(); listIterator.next(); listIterator.remove();//remove element at 5th position 
+25
May 23 '12 at 6:06 a.m.
source share
  public void DeleteUserIMP(UserIMP useriamp) { synchronized (ListUserIMP) { if (ListUserIMP.isEmpty()) { System.out.println("user is empty"); } else { Iterator<UserIMP> it = ListUserIMP.iterator(); while (it.hasNext()) { UserIMP user = it.next(); if (useriamp.getMoblieNumber().equals(user.getMoblieNumber())) { it.remove(); System.out.println("remove it"); } } // ListUserIMP.remove(useriamp); System.out.println(" this user removed"); } Constants.RESULT_FOR_REGISTRATION = Constants.MESSAGE_OK; // System.out.println("This user Deleted " + Constants.MESSAGE_OK); } } 
+10
Jan 24 '13 at 11:58
source share
 String[] mString = new String[] {"B", "D", "F"}; for (int j = 0; j < mString.length-1; j++) { List_Of_Array.remove(mString[j]); } 
+4
May 23 '12 at 6:01
source share

As mentioned earlier

 iterator.remove() 

perhaps the only safe way to remove list items during a loop.

For a deeper understanding of deleting items with an iterator, try looking at this thread.

+4
Feb 24 '15 at 8:59
source share

How about this? Just think -

 import java.util.ArrayList; class Solution { public static void main (String[] args){ ArrayList<String> List_Of_Array = new ArrayList<String>(); List_Of_Array.add("A"); List_Of_Array.add("B"); List_Of_Array.add("C"); List_Of_Array.add("D"); List_Of_Array.add("E"); List_Of_Array.add("F"); List_Of_Array.add("G"); List_Of_Array.add("H"); int i[] = {1,3,5}; for (int j = 0; j < i.length; j++) { List_Of_Array.remove(i[j]-j); } System.out.println(List_Of_Array); } } 

And the result was -

 [A, C, E, G, H] 
+2
May 23 '12 at 6:03 a.m.
source share

Try it,

 ArrayList<String> List_Of_Array = new ArrayList<String>(); List_Of_Array.add("A"); List_Of_Array.add("B"); List_Of_Array.add("C"); List_Of_Array.add("D"); List_Of_Array.add("E"); List_Of_Array.add("F"); List_Of_Array.add("G"); List_Of_Array.add("H"); int i[] = {5,3,1}; for (int j = 0; j < i.length; j++) { List_Of_Array.remove(i[j]); } 
+2
May 23 '12 at 6:07 am
source share

remove (int index) arraylist method removes the item at the specified position (index) in the list. After deleting the elements of the array, any subsequent elements are shifted to the left.

So if the arrailist contains {20,15,30,40}

I called the method: arraylist.remove (1)

then data 15 will be deleted, and 30 and 40 these two elements will be shifted by 1.

For this reason, you need to remove the higher arraylist index element first.

So, for your specific situation ... the code will be ..

 ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); list.add("F"); list.add("G"); list.add("H"); int i[] = {1,3,5}; for (int j = i.length-1; j >= 0; j--) { list.remove(i[j]); } 
+2
Mar 06 '16 at 14:28
source share

I assume that the array I am a popup, and here is another solution with Iterator, it is more general:

 ArrayList<String> list = new ArrayList<String>(); list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); list.add("F"); list.add("G"); list.add("H"); int i[] = {1,3,5}; Iterator<String> itr = list.iterator(); int pos = 0; int index = 0; while( itr.hasNext() ){ itr.next(); if( pos >= i.length ){ break; } if( i[pos] == index ){ itr.remove(); pos++; } index++; } 
+2
Aug 29 '16 at
source share

If you use "=", a replica is created for the original arraylist in the second, but the link is the same, so if you change one list, the other will also be changed. Use this instead of "="

  List_Of_Array1.addAll(List_Of_Array); 
+1
Nov 19 '13 at 5:32
source share



All Articles