Java Arraylist removes multiple items by index

Here is my code:

for (int i = 0; i < myarraylist.size(); i++) {
        for (int j = 0; j < stopwords.size(); j++) {
            if (stopwords.get(j).equals(myarraylist.get(i))) {
                myarraylist.remove(i);
                id.remove(i);
                i--; // to look at the same index again!
            }
        }
    }

I have a problem. After deleting an element, the whole index always changed, the cycle above is so dirty.

To illustrate: I have 54 data, but the loop above becomes messy after deleting the item. So only 50 data are checked.

Is there any other way or fix my code before deleting multiple items by index ? it is so important for me to remove another arraylist that has the same index.

+5
source share
7 answers

: ArrayLists, , , Arrays. , , ArrayLists.

, , ArrayList ( - ) , ArrayList .

ArrayList ArrayList.

for(int x = arrayList.size() - 1; x > 0; x--)
{
    arrayList.remove(x);
}

, . ArrayList API , .

+6

Iterator.remove() .

for (Iterator<String> iter = myarraylist.iterator(); iter.hasNext(); ) {
  String element = iter.next();
  if (element meets some criteria) {
    iter.remove();
  }
}

Google Guava filter, .

Iterable<String> filtered = Iterables.filter(myarraylist, new Predicate<String>() {
  public boolean apply(String element) {
    return true of false based on criteria
  }
});
+10

, . java.util.Collection # removeAll

+1

, , (untested):

int dynamicSize = myarraylist.size();
for (int i = 0; i < dynamicSize; i++) {
        for (int j = 0; j < stopwords.size(); j++) {
            if (stopwords.get(j).equals(myarraylist.get(i))) {
                myarraylist.remove(i);
                id.remove(i);
                i--; // to look at the same index again!
                dynamicSize--;
            }
        }
    }
0

, .

Iterator myListItr = myarraylist.iterator();
//Iterate on list 
    while(myListItr .hasNext()) {
    //if stop words is list of string then contains will work as it is. If its your custom object then override equals method.
        if(stopwords.contains( myListItr.next())){
    myListItr.remove();
}
}
0

ListIterator

ArrayList<String> list = new ArrayList<String>();
      // List : ["java", ".net", "javascript", "html", "css", "selenium", "image", "Spring"]

    ArrayList<Integer> indexes = new ArrayList<Integer>();
                                      // indexes : [5, 3, 2, 5, 0]
    // Sort the Indexes in Order to remove from back words. and make Unique
    TreeSet<Integer> uniqueSorted = new TreeSet<Integer>();
        uniqueSorted.addAll(indexes);

    // To remove all elements from the ArrayList and make its size = 0  
        indexes.clear(); 
        indexes.addAll(uniqueSorted);

    // Iterate form back, so that if size decreases also no problem.
    ListIterator<Integer> li = indexes.listIterator(indexes.size());

   // we can traverse a List in both the directions (forward and Backward).
        while(li.hasPrevious()) {
            int position = li.previous();
            list.remove(position);
        }
0

, .

data = [5,2,7,3,9,34,63,23,85,23,94,7]

indexes to remove

int index[] = [5,2,7,9]

Note . When you remove one element from the array, the other elements are shifted by 1.

If we use ArrayList to remove index elements, then first you need to sort the indexes in descending order.

those. indexes = [9,7,5,2], then remove the item from the index

ArrayList<Integer> data = Arrays.asList(new Integer[] {5,2,7,3,9,34,63,23,85,23,94,7});

for(int i=0;i<index.length;i++){
    data.remove(index[i]);
    }
0
source

All Articles