I want to create a simple 2D histogram from array of points.
Class Points
import java.util.ArrayList;
import java.util.List;
public class Points {
static List<List<Integer>> histogram = new ArrayList<List<Integer>>();
public static void createHistogram(List<Point> point,int max) {
for(int x = 0; x < max; x++) {
histogram.add(new ArrayList<Integer>());
for(int y = 0; y < max; y++) {
histogram.get(x).add(0);
System.out.print((histogram.get(x).get(y)) +" ");
}
System.out.println();
}
for(int x = 0; x < point.size(); x++)
histogram.get(point.get(x).x).get(point.get(x).y)++;
}
public static void main(String[] args) {
List<Point> points = new ArrayList<Point>();
points.add(new Point(0,10));
points.add(new Point(1,2));
points.add(new Point(2,5));
points.add(new Point(1,2));
createHistogram(points,10);
}
}
Point Class
public class Point{
public int x = 0;
public int y = 0;
Point(int x, int y){
this.x = x;
this.y = y;
}
}
I get "Invalid argument to operation ++/--" error when I try to increment the value of histogram. Why is that? When I print the value of "histogram.get(point.get(x).x).get(point.get(x).y)" there is no issue. Why changing its value is not permitted? How can I fix it?