简体   繁体   English

修改IntList的ArrayList中的Int

[英]Modifying Ints in ArrayList of IntLists

In processing, I am attempting to create an ArrayList of IntLists such that the initial number of Elements in the ArrayList is variable to begin with (but I will not change it in the code) and the number of elements in the IntList is initially 0 but will be added to and edited within the code. 在处理过程中,我尝试创建一个IntList的ArrayList,以使ArrayList中元素的初始数量是可变的(但我不会在代码中更改),并且IntList中的元素数量最初为0,但将被添加到代码中并在其中进行编辑。 My code is a Japanese Ladder game where the ArrayList is of Ladders, each of which has an IntList containing "Rungs" which are int components that correspond to a Y location on the ladder. 我的代码是一个日语梯子游戏,其中ArrayList是梯子,每个梯子都有一个包含“梯级”的IntList,梯级是对应于梯子上Y位置的int分量。

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);
}

So my problem is that addRung() seems to add 50 to every single element ofthe ArrayList and "print(Ladders);" 所以我的问题是addRung()似乎给ArrayList和“ print(Ladders);”的每个元素都加了50。 outputs... 输出...

[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 ]]

Is there a better way to go about this entirely or am I doing something wrong? 有没有更好的方法可以完全解决此问题,或者我做错了什么?

I included an image of what my code is outputting... It should be a single rung at location 50 but it is 9 rungs at 50. 我包括了我的代码输出的图像...应该在位置50处为单个梯级,但在位置50为9个梯级。

图片

Please think carefully about exactly what this code is doing: 请仔细考虑以下代码的确切作用:

for(int i=0;i<numRails-1;i++)
  {
    Ladders.add(new IntList());
    temp.clear();
    temp.append(0);
    Ladders.set(i,temp);
  }

Here you're adding a new IntList to your Ladders ArrayList . 在这里,您要将新的IntList添加到Ladders ArrayList Then you're clearing temp and adding 0 to it, and then you're setting the index (which you just added a new IntList to) to temp , which replaces the new IntList you just added. 然后,您要清除temp并将其添加为0 ,然后将索引(您刚刚向其中添加了一个新的IntList )设置为temp ,它将替换您刚刚添加的新IntList

In other words, you're adding temp to every index of the ArrayList . 换句话说,您将temp添加到ArrayList每个索引中。 That's just a bunch of references to the same IntList , which is why when you add a value to one index, it adds the value to every index. 那只是对同一IntList的一堆引用,这就是为什么当您向一个索引中添加一个值时,它会将该值添加到每个索引中。

To fix this, you need to get rid of your temp variable completely. 要解决此问题,您需要完全摆脱temp变量。 Just add a new IntList to each index, and then use the get() function to get the IntList from the index in the ArrayList . 只需向每个索引添加一个新的IntList ,然后使用get()函数从ArrayList的索引中获取IntList It would look like this: 它看起来像这样:

ladders.get(index).append(value);

As a side note, you should really get into the habit of 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 instead of disconnected snippets. 然后,如果遇到问题,可以发布MCVE而不是断开连接的代码段。

You should also try to use standard naming conventions: variables should start with a lower case letter. 您还应该尝试使用标准的命名约定:变量应以小写字母开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM