ArrayList not behaving as expected

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.

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

1 Like

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.

3 Likes

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


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


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…

1 Like



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
//
2 Likes

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

type or paste code here

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

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

1 Like

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

Thanks for the quick help everyone.

1 Like

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

1 Like

Done, Looks much prettier. Thanks

2 Likes