大家.
我刚刚进入Java,我正在尝试编写一个简单的游戏,敌人在网格上追逐玩家.我在寻路上使用维基百科页面中的简单算法进行寻路.这涉及创建两个列表,每个列表项包含3个整数.这是我正在尝试构建和显示这样一个列表的测试代码.
当我运行以下代码时,它会为ArrayList中的每个数组打印出相同的数字.为什么这样做?
public class ListTest {
public static void main(String[] args) {
ArrayList<Integer[]> list = new ArrayList<Integer[]>();
Integer[] point = new Integer[3];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
point[j] = (int)(Math.random() * 10);
}
//Doesn't this line add filled Integer[] point to the
//end of ArrayList list?
list.add(point);
//Added this line to confirm that Integer[] point is actually
//being filled with 3 random ints.
System.out.println(point[0] …Run Code Online (Sandbox Code Playgroud)