Is their a prebuilt method that works differently than removeAll
to remove all of a certain value in an array list?
I have a method to delete duplicates in arrayLists that looks like this:
The first 2 for for loops work and set it correctly but I don't know what to do for the second for loop to delete all values of -1 from the ArrayList or is their a better way to do this?
public static ArrayList<Integer> deleteDuplicates(ArrayList<Integer> a) {
for (int i = 0; i < a.size(); i++) {
for (int j = (i + 1); j < a.size(); j++) {
if (a.get(i) == a.get(j) && i != j) {
a.set(j, -1);
}
}
}
for (int i = 0; i < a.size(); i++) {
if (a.get(i) == -1) {
a.removeAll(int -1);
}
}
The removeAll(int -1)
thing at the bottom doesn't compile, I just left it there so you can see what I'm trying to do.
EDIT:
replaced the for loop at the bottom with this:
for (int i = 0; i < a.size(); i++) {
if (a.get(i) == -1) {
a.remove(i);
i--;
}
}
pretty sure that works.