# Add an Array to an ArrayList

**URL:** <https://discourse.processing.org/t/add-an-array-to-an-arraylist/16884>\
**Category:** Beginners\
**Created:** [January 5, 2020, 10:05pm UTC](https://discourse.processing.org/t/add-an-array-to-an-arraylist/16884 "2020-01-05T22:05:34Z")\
**Posts on this page:** 3\
**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:** [January 5, 2020, 10:05pm UTC](https://discourse.processing.org/t/add-an-array-to-an-arraylist/16884/1 "2020-01-05T22:05:34Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [January 5, 2020, 10:25pm UTC](https://discourse.processing.org/t/add-an-array-to-an-arraylist/16884/2 "2020-01-05T22:25:38Z")

</div>

Hello,

I was tinkering with this a week ago…

Hope this helps:

```auto
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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d65a0545f9de2dfdec5ab2e3963d9549caf94d7b.png)

🙂

---

<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:** [January 5, 2020, 10:51pm UTC](https://discourse.processing.org/t/add-an-array-to-an-arraylist/16884/3 "2020-01-05T22:51:20Z")

</div>

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.
