Hello only have a few hours with Java. (from Python)
I am trying to define a multidimensional array and populate it with the “add” method. however, I am seeing some bizarre results:
List<String[]> DrawInstructions = new ArrayList<String[]>();
String[] pair = {"",""};
pair[0]="UW";
pair[1]="100";
DrawInstructions.add(pair);
pair[0]="UM";
pair[1]="10";
DrawInstructions.add(pair);
pair[0]="UT";
pair[1]="50";
DrawInstructions.add(pair)
I expected DrawInstructions to end up with this:
[(“UW,”100”),(“UM”,”10″),(“UT”,”50″)]
But instead I am getting:
[(“UT”,”50″),(“UT”,”50″),(“UT”,”50″)]
I am sure this is pretty elemental but I cant figure it out, I have searched for a couple of hours. Thank you for any advice.
You are adding the same array (pair) three times. All you do is changing it’s value(content).
Remember that java operates on references, not on the copies of the objects.
Try to create pair1, pair2, pair3.