问题
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 problem.. after element removed, all index always changed, the loop above so messy.
To illustrate: I have 54 data, but loop above become messy after element removed.. so only 50 data that checked.
Is there another way or fix my code to remove multiple element by index?? element index is so important to me, to remove another arraylist that have the same index.
回答1:
One thing you need to keep in mind is that when you use ArrayLists
that they are meant to be versatile, moreso than Arrays
. You can shorten an array by removing an entire index, add an index to it, and do wonderfulness with ArrayLists
.
This is a common problem with people who do not realize, or remember, that when you remove a value, the ArrayList
indexes (or whatever the correct plural is) readjust and the ArrayList
shortens.
When attempting to remove elements from an ArrayList
, you should always start at the end of the ArrayList
.
for(int x = arrayList.size() - 1; x > 0; x--)
{
arrayList.remove(x);
}
This should provide you with the function that you are looking for. Take a look at the ArrayList API for other methods that may help you.
回答2:
Use Iterator.remove()
to remove elements while iterating.
for (Iterator<String> iter = myarraylist.iterator(); iter.hasNext(); ) {
String element = iter.next();
if (element meets some criteria) {
iter.remove();
}
}
Or use Google Guava's filter which returns a filtered view and leaves the original list unchanged.
Iterable<String> filtered = Iterables.filter(myarraylist, new Predicate<String>() {
public boolean apply(String element) {
return true of false based on criteria
}
});
回答3:
looks like as you want to remove one collection from another.. You should use java.util.Collection#removeAll method instead
回答4:
Not sure about why you want to do this by index, but you can try this (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--;
}
}
}
回答5:
while iterating list and you are removing the object use iterator.
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();
}
}
回答6:
Using 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);
}
回答7:
Ok this is very good problem to solve. Lets start with an exmaple
We have data = [5,2,7,3,9,34,63,23,85,23,94,7]
indexes to remove
int index[] = [5,2,7,9]
Note : As you remove single item from array other elements get shifted by 1.
If we use ArrayList to remove elements of indexes then first you need to sort the indexes in descending.
i.e indexes = [9,7,5,2] then remove the element from 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]);
}
来源:https://stackoverflow.com/questions/29656071/java-arraylist-remove-multiple-element-by-index