Add an Array to an ArrayList

I’ve created a REALLY simple code snippet to show what I’m trying to do. I need to add an array that I have defined into an ArrayList. I need an ArrayList as I don’t know how many arrays I will end up with. This will Obvs be in a loop but I just wanted to show the problem I’m having

When I run the following

float[] stuff = {1,2,3}; 
ArrayList <float[]> list = new ArrayList<float[]>();
list.add(stuff);
printArray(list);

I am hoping to see 1,2,3 in the first entry for the ArrayList. instead I just see a java lang object.

Found some references for addAll but that did get me any further.

Mucho thanks in advance.

1 Like

Hello,

I was tinkering with this a week ago…

Hope this helps:

float[] stuff1 = {1, 2, 3}; 
float[] stuff2 = {4, 5, 6}; 
ArrayList <float[]> list = new ArrayList<float[]>();
list.add(stuff1);
list.add(stuff2);
printArray(list);
println();

//Print Test 1
println(list.get(0));
println(list.get(1));
println();

//Print Test 2
for (int i=0; i< 2; i++) 
  {
  println(list.get(i));
  }
println();

 //Print Test 3  
for (float[] i : list) 
  {
  println(i);  
  }
println();  

Output:

:slight_smile:

2 Likes

Brilliant. That nails it.

I actually had it but I was being fooled by the debugger. It hadn’t listed the elements and therefore, incorrectly assumed that it hadn’t work.

Thanks for the help VERY much appreciated.

3 Likes