# How do i convert array to ArrayList?

**URL:** <https://discourse.processing.org/t/how-do-i-convert-array-to-arraylist/42839>\
**Category:** Beginners\
**Created:** [September 26, 2023, 4:47pm UTC](https://discourse.processing.org/t/how-do-i-convert-array-to-arraylist/42839 "2023-09-26T16:47:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AniManOtaku](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/animanotaku/32/18733_2.png) [@AniManOtaku](https://discourse.processing.org/u/AniManOtaku)\
**Post date:** [September 26, 2023, 4:47pm UTC](https://discourse.processing.org/t/how-do-i-convert-array-to-arraylist/42839/1 "2023-09-26T16:47:28Z")

</div>

Please i need to know

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 26, 2023, 5:18pm UTC](https://discourse.processing.org/t/how-do-i-convert-array-to-arraylist/42839/2 "2023-09-26T17:18:09Z")

</div>

There are many ways to do it. 1 to them is to use _java.util_.Arrays.[**asList()**](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Arrays.html#asList(T...)).  
Then you can use that returned fixed-size List as the passed argument for a `new` [ArrayList](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html#%3Cinit%3E(java.util.Collection)).

---

<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:** [September 26, 2023, 5:28pm UTC](https://discourse.processing.org/t/how-do-i-convert-array-to-arraylist/42839/3 "2023-09-26T17:28:21Z")

</div>

manual

```auto

int [] arr1 = {
  33,
  23,
  11
};

ArrayList<Integer> arrList;

arrList = new ArrayList();

for (int n = 0; n < arr1.length; n++) {
  arrList.add( arr1[n] );
}

arr1=null;

for (int i : arrList) {
  println(i);
}

```

---

<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:** [September 26, 2023, 5:36pm UTC](https://discourse.processing.org/t/how-do-i-convert-array-to-arraylist/42839/4 "2023-09-26T17:36:34Z")

</div>

> [@GoToLoop](#):
>
> Then you can use that returned fixed-size List as the passed argument for a `new` [ArrayList](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/ArrayList.html#%3Cinit%3E(java.util.Collection)).

gotoLoop’s way

unfortunately, in the for loop at the end I wasn’t able to say

```auto
for (Integer i : arrList) {

```

```auto
import java.util.*;

int [] arr1 = {
  33,
  23,
  11
};

ArrayList<Integer> arrList;

size(660, 660);

arrList = new ArrayList(Arrays.asList(arr1));

//for (int n = 0; n < arr1.length; n++) {
// arrList.add( arr1[n] );
//}

arr1=null;

for (Object i : arrList) {
  println(i);
}
//

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [September 26, 2023, 6:04pm UTC](https://discourse.processing.org/t/how-do-i-convert-array-to-arraylist/42839/5 "2023-09-26T18:04:04Z")

</div>

> [@Chrisir](#):
>
> Unfortunately, in the for loop at the end, I wasn’t able to say…

For arrays of primitive type such as `int[]`, **asList()** won’t work.

A manual approach like yours would be OK then:

```auto
final int[] arr = { 10, 20, 30 };
final ArrayList<Integer> arrList = new ArrayList<Integer>(arr.length);

for (final int num : arr) arrList.add(num);

println(str(arr));
println(arrList);

exit();

```
