修改 IntList 的 ArrayList 中的 Int
Modifying Ints in ArrayList of IntLists
在处理过程中,我试图创建一个 IntList 的 ArrayList,这样 ArrayList 中元素的初始数量开始时是可变的(但我不会在代码中更改它)和IntList 最初为 0,但将被添加到代码中并在代码中进行编辑。
我的代码是一个日本梯子游戏,其中 ArrayList 是梯子,每个梯子都有一个包含 "Rungs" 的 IntList,它们是对应于梯子上 Y 位置的整数组件。
ArrayList <IntList> Ladders = new ArrayList <IntList>();
IntList temp = new IntList();
void setup()
{
for(int i=0;i<numRails-1;i++)
{
Ladders.add(new IntList());
temp.clear();
temp.append(0);
Ladders.set(i,temp);
}
}
void addRung (int spot)
{
temp.clear();
temp = Ladders.get(spot);
temp.append(50);
//note that 50 is an arbitrary number, it would be given by MouseY
Ladders.set(spot,temp);
print(Ladders);
//I have also tried...
Ladders.get(spot).append(50);
}
所以我的问题是 addRung() 似乎将 50 添加到 ArrayList 和 "print(Ladders);" 输出的每个元素...
[IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ]]
有没有更好的方法来完全解决这个问题,还是我做错了什么?
我包含了我的代码正在输出的图像...它应该是位置 50 处的单个梯级,但在 50 处是 9 个梯级。
请仔细思考这段代码究竟在做什么:
for(int i=0;i<numRails-1;i++)
{
Ladders.add(new IntList());
temp.clear();
temp.append(0);
Ladders.set(i,temp);
}
在这里,您要向 Ladders
ArrayList
添加一个新的 IntList
。然后你清除 temp
并向其添加 0
,然后你将索引(你刚刚添加了一个新的 IntList
)设置为 temp
,这替换您刚刚添加的新 IntList
。
换句话说,您要将 temp
添加到 ArrayList
的每个索引。这只是对同一个 IntList
的一堆引用,这就是为什么当您向一个索引添加一个值时,它会将该值添加到每个索引。
要解决此问题,您需要完全删除 temp
变量。只需为每个索引添加一个新的IntList
,然后使用get()
函数从ArrayList
中的索引中获取IntList
。它看起来像这样:
ladders.get(index).append(value);
附带说明一下,您真的应该养成 debugging your code before asking a question. A few print statements would have gone a long way to helping you figure out what's going on. Then if you get stuck, you can post a MCVE 而不是不连贯的片段的习惯。
您还应尝试使用标准命名约定:变量应以小写字母开头。
在处理过程中,我试图创建一个 IntList 的 ArrayList,这样 ArrayList 中元素的初始数量开始时是可变的(但我不会在代码中更改它)和IntList 最初为 0,但将被添加到代码中并在代码中进行编辑。 我的代码是一个日本梯子游戏,其中 ArrayList 是梯子,每个梯子都有一个包含 "Rungs" 的 IntList,它们是对应于梯子上 Y 位置的整数组件。
ArrayList <IntList> Ladders = new ArrayList <IntList>();
IntList temp = new IntList();
void setup()
{
for(int i=0;i<numRails-1;i++)
{
Ladders.add(new IntList());
temp.clear();
temp.append(0);
Ladders.set(i,temp);
}
}
void addRung (int spot)
{
temp.clear();
temp = Ladders.get(spot);
temp.append(50);
//note that 50 is an arbitrary number, it would be given by MouseY
Ladders.set(spot,temp);
print(Ladders);
//I have also tried...
Ladders.get(spot).append(50);
}
所以我的问题是 addRung() 似乎将 50 添加到 ArrayList 和 "print(Ladders);" 输出的每个元素...
[IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ], IntList size=1 [ 50 ]]
有没有更好的方法来完全解决这个问题,还是我做错了什么?
我包含了我的代码正在输出的图像...它应该是位置 50 处的单个梯级,但在 50 处是 9 个梯级。
请仔细思考这段代码究竟在做什么:
for(int i=0;i<numRails-1;i++)
{
Ladders.add(new IntList());
temp.clear();
temp.append(0);
Ladders.set(i,temp);
}
在这里,您要向 Ladders
ArrayList
添加一个新的 IntList
。然后你清除 temp
并向其添加 0
,然后你将索引(你刚刚添加了一个新的 IntList
)设置为 temp
,这替换您刚刚添加的新 IntList
。
换句话说,您要将 temp
添加到 ArrayList
的每个索引。这只是对同一个 IntList
的一堆引用,这就是为什么当您向一个索引添加一个值时,它会将该值添加到每个索引。
要解决此问题,您需要完全删除 temp
变量。只需为每个索引添加一个新的IntList
,然后使用get()
函数从ArrayList
中的索引中获取IntList
。它看起来像这样:
ladders.get(index).append(value);
附带说明一下,您真的应该养成 debugging your code before asking a question. A few print statements would have gone a long way to helping you figure out what's going on. Then if you get stuck, you can post a MCVE 而不是不连贯的片段的习惯。
您还应尝试使用标准命名约定:变量应以小写字母开头。