public Point{
int x, y;
public Point(int x, int y){
int x = x;
int y = y;
}
}
ArrayList<Point> points = new ArrayList<>();
// add points by using points.add() then use this loop
ArrayList<ArrayList<Point>> A = new ArrayList<>();
for (int i = 0; i < 3; i ) {
A.add(points);
}
I have an arraylist of arraylists that each of them have the same points For example when you print this is the output
ArrayList<ArrayList<Point>> A = [[(0,10), (20,3), (2,5), (2,8)], [(0,10), (20,3), (2,5), (2,8)], [(0,10), (20,3), (2,5), (2,8)]]
I want to remove all the points that their x value is not equal to the index of the array list before printing
A.get(0) = [(0,10)]
A.get(1) = []
A.get(2) = [(2,5),(2,8)]
so the output should be
[[(0,10)], [], [(2,5), (2,8)]]
I tried to use remove method and for loop and other ways but they didn't work.
for(int i = 0; i < list.size();i ){
if(list.get(i).x != 0){
list.remove(i--);
}
}
Is there anyone who can help?
CodePudding user response:
You are probably looking for something like this:
int index = 0;
for (ArrayList<Point> list : A) {
list.removeIf(point -> point.x != index);
index ;
}
index variable could be removed as well but I didn't want to oversimplify the code, to preserve clarity.
CodePudding user response:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class teste {
public static void main(String[] args) {
ArrayList<Point> points = new ArrayList<>();
points.add(new Point(0, 10));
points.add(new Point(9, 10));
points.add(new Point(20, 3));
System.out.println(points);
//solution 1
List<Point> list = points.stream().filter(o -> !o.equals(9, 10)).collect(Collectors.toList());
System.out.println(list);
//solution 2
List<Point> list2 = new ArrayList<>();
points.stream().forEach(o -> {
if (o.equals(9, 10)) {
list2.add(null);
} else {
list2.add(o);
}
});
System.out.println(list2);
}
public static class Point {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "(" x ", " y ")";
}
public boolean equals(int x, int y) {
return this.x == x && this.y == y;
}
}
}
[(0, 10), (9, 10), (20, 3)]
[(0, 10), (20, 3)]
[(0, 10), null, (20, 3)]
CodePudding user response:
Here is one way. To account for change in indexed position when removing items, start at the end and move backward.
- iterate over the list of lists.
- for each inner list, iterate backwards and remove the unwanted points
for (int i = 0; i < lists.size(); i ) {
List<Point> points = lists.get(i);
for (int k = points.size()-1; k >= 0; k--) {
if (points.get(k).x != i) {
points.remove(k);
}
}
}
Given the current data, here is the output.
[[(0, 10)], [], [(2, 5), (2, 8)]]
Here is a stream version if you're interested. This doesn't explicitly remove anything. It just filters out the unwanted values.
- stream the indices of the outer list
- using map, stream the inner list.
- filter out the unwanted points
- and repackage into a list of lists
List<List<Point>> result = IntStream.range(0,lists.size())
.mapToObj(i->lists.get(i).stream()
.filter(point->point.x == i)
.toList()).toList();