[英]Why am I getting an error when I try to create the Scoreboard object with an array?
[英]When I try to change a object in a specified array index, it changes the entire array with that object. Any reasons why?
当我尝试使用现有对象设置指定数组时,数组中的其他索引会更改。 当我在2D数组中时,我想要更改的整行将使用相同的对象进行更改。
我没有尝试任何新的东西。 只是彻底搜索我的代码中的问题。
更新:我甚至尝试用0替换y,但整个阵列仍然填满!
public void addPart(Part p, int y) {
// Adds "p" to the ArrayList of every element in components[], but I only want it in the "y" index.
components[y].add(p);
}
在Eclipse调试器中,我发现数组的每个元素都已更改,但我希望只更改所需的元素。
澄清编辑: components[]
是一维数组。
这是因为Arrays.fill()方法。 当您更改数组中的某些内容时,其他内容会更改,因为它们引用同一个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.