I have a nested ArrayList as below with boolean values. I want to remove for example 3rd item from all the rows. I tried with a loop but it doesnt resolve remove
as a method. How should I do this? Many thanks for your help.
for (int i = 0; i < list.size(); i++){
list.get(i).remove(3)// this remove method shows as an error in IDE
}
true false true false false false
false false true false true true
... It's a list of
List<Instance> listInstances = new ArrayList<Instance>();
and the classInstance
hasvals = new ArrayList<Boolean>();
....
In this case your solution can look like :
public static Instance deleleNthElement(Instance instance, int index) {
instance.getVals().remove(index - 1);
return instance;
}
then with stream you can call the method like so :
int index = 3;
listInstances = listInstances.stream()
.map(instance -> deleleNthElement(instance, index))
.collect(Collectors.toList());
I see no error in your logic, I believe you are missing a ';' from the end of the remove(3).
By the way, List is an Interface, you will need to instanciate as an ArrayList (or some such).
I strung the following together, seems to do what you intended:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
List<Boolean> row1 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] {true,false,true,true}));
List<Boolean> row2 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] {true,true,false,true}));
List<List<Boolean>> list = Arrays.asList(new ArrayList[] {(ArrayList) row1, (ArrayList) row2});
for (int i=0;i<list.size();i++){
list.get(i).remove(3);// this remove method shows as an error in IDE
}
for (List<Boolean> ll : list) {
for (Boolean l : ll) {
System.out.print(l + ",");
}
System.out.println();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.