Here is my code:
for (int i = 0; i < myarraylist.size(); i++) {
for (int j = 0; j < stopwords.size(); j++) {
if (stopwords.get(j).e
looks like as you want to remove one collection from another.. You should use java.util.Collection#removeAll method instead
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--;
}
}
}
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.
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
}
});
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);
}
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]);
}