# ArrayList not behaving as expected

**URL:** <https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724>\
**Category:** Beginners\
**Created:** [December 30, 2019, 11:22am UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724 "2019-12-30T11:22:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psteely](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psteely/32/7647_2.png) [@Psteely](https://discourse.processing.org/u/Psteely)\
**Post date:** [December 30, 2019, 11:22am UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/1 "2019-12-30T11:22:45Z")

</div>

Hi Folks, I’m new to this coding lark as well as Processing so I’m sure this is just me being dumb but I can’t figure it. I have created a small subset of my code to more easily explain it.

```auto
ArrayList<trainData> td;
void setup () {
  td = new ArrayList<trainData>(2);
  float[] in = new float[2];
  in[0] = 1.0;
  in[1] = 1.0;
  td.add( new trainData(in));
  // in = new float[2];
  in[0] = 2;
  in[1] = 3;
  td.add( new trainData(in));
  for (trainData d : td) {
    println(d.Testinputs);
  }
}
class trainData {
  float[] Testinputs;
  trainData(float[] i_) {
    Testinputs = i_;
  }
}

```

If I run the code as above I get {2,3} in both entries in my ArrayList. If I remove the comment “// in = new float[2];” and rerun it I get {1,1} in the first entry in the Array List and {2,3} in the second. This is what I expected to get from the first run.  
When I debug it with the comment in as soon as it runs “in[0] = 2;” it updates the Testinputs in the first entry.  
I would have thought the first entry in the ArrayList should safe and sound in the ArrayList by the time this line executes.

I can clearly work around the problem but what am I missing.

Any help appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [December 30, 2019, 12:37pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/2 "2019-12-30T12:37:25Z")

</div>

Processing / Java object references are basically pointers. You pass in this reference to `trainData` and assign it to `Testinput` but they are all referring to the same actual array. Unless you copy the array (eg. `i_.clone()`) or create one using `new` every reference is to the same array, and so if you change the values through one reference you change the values everywhere that array is referenced.

Incidentally, it is conventional to name classes starting uppercase, and fields starting lowercase - eg. `TrainData` and `testInputs`.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 30, 2019, 12:48pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/3 "2019-12-30T12:48:12Z")

</div>

but at first i would like to ask you to format your code `</> code `  
like

```auto

ArrayList td = new ArrayList();
float[] in = { 1.0, 1.0 };

void setup () {
  td.add( in );
  in = new float[2];
  in[0] = 2;
  in[1] = 3;
  td.add( in );
  println( td.size() );
  for ( Object b : td ) println(b);
}  

```

second it helps us to understand, if you include the printed info

```auto

2
[0] 1.0
[1] 1.0
[0] 2.0
[1] 3.0

```

in this i skipped your class, just a first step what you might want to do…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 30, 2019, 12:52pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/4 "2019-12-30T12:52:03Z")

</div>

```auto

ArrayList<TrainData> td = new ArrayList();

void setup () {
  size(650, 650);

  float[] in = new float[2];
  in[0] = 1.0;
  in[1] = 1.0;
  td.add( new TrainData(in) );

  in = new float[2];
  in[0] = 2;
  in[1] = 3;
  td.add( new TrainData(in) );

  for (TrainData d : td) {
    printArray(d.testinputs);
  }//for
  //
}//func 

// ========================================================================

class TrainData {

  float[] testinputs;

  //constr 
  TrainData(float[] i_) {
    testinputs = i_.clone();
  }//constr 
  //
}//class
//

```

---

<div class="post-metadata">

**Author:** ![Psteely](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psteely/32/7647_2.png) [@Psteely](https://discourse.processing.org/u/Psteely)\
**Post date:** [December 30, 2019, 6:10pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/5 "2019-12-30T18:10:27Z")

</div>

OK thanks, knew it would be some mis understanding on my part. Thanks for the naming convention tip. My code is now changed.

---

<div class="post-metadata">

**Author:** ![Psteely](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psteely/32/7647_2.png) [@Psteely](https://discourse.processing.org/u/Psteely)\
**Post date:** [December 30, 2019, 6:12pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/6 "2019-12-30T18:12:51Z")

</div>

> [@Psteely](#):
>
> ArrayList td;  
> void setup () {  
> td = new ArrayList(2);  
> float in = new float[2];  
> in[0] = 1.0;  
> in[1] = 1.0;  
> td.add( new trainData(in));  
> // in = new float[2];  
> in[0] = 2;  
> in[1] = 3;  
> td.add( new trainData(in));  
> for (trainData d : td) {  
> println(d.Testinputs);  
> }  
> }  
> class trainData {  
> float Testinputs;  
> trainData(float i\_) {  
> Testinputs = i\_;  
> }  
> }

```auto
type or paste code here

```

---

<div class="post-metadata">

**Author:** ![Psteely](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psteely/32/7647_2.png) [@Psteely](https://discourse.processing.org/u/Psteely)\
**Post date:** [December 30, 2019, 6:13pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/7 "2019-12-30T18:13:40Z")

</div>

How do I do this? Soz, bit of a newb…

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 30, 2019, 6:15pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/8 "2019-12-30T18:15:16Z")

</div>

Prior to posting remember to hit **ctrl-t** in processing to get auto indents

Then copy paste into the forum

Select entire code section with mouse

Hit `</>` in the small command bar

---

<div class="post-metadata">

**Author:** ![Psteely](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psteely/32/7647_2.png) [@Psteely](https://discourse.processing.org/u/Psteely)\
**Post date:** [December 30, 2019, 6:17pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/9 "2019-12-30T18:17:03Z")

</div>

Got it. Thanks. I’m sure I will be needing to do this many more times…

Thanks for the quick help everyone.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 30, 2019, 6:18pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/10 "2019-12-30T18:18:36Z")

</div>

You can also go back and edit your old post: Click on the small pen a the bottom of the post

---

<div class="post-metadata">

**Author:** ![Psteely](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/psteely/32/7647_2.png) [@Psteely](https://discourse.processing.org/u/Psteely)\
**Post date:** [December 30, 2019, 6:21pm UTC](https://discourse.processing.org/t/arraylist-not-behaving-as-expected/16724/11 "2019-12-30T18:21:20Z")

</div>

Done, Looks much prettier. Thanks
