【发布时间】:2019-01-01 15:44:13
【问题描述】:
我想从点数组创建一个简单的二维直方图。
班级积分
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);
}
}
点类
public class Point{
public int x = 0;
public int y = 0;
Point(int x, int y){
this.x = x;
this.y = y;
}
}
当我尝试增加直方图的值时,出现“操作参数无效 ++/--”错误。这是为什么?当我打印“histogram.get(point.get(x).x).get(point.get(x).y)”的值时,没有问题。为什么不允许更改其值?我该如何解决?
【问题讨论】:
-
histogram.get(point.get(x).x).get(point.get(x).y)++;到底应该增加什么?
标签: java arrays list arraylist