# How to do an array of IntLists?

**URL:** <https://discourse.processing.org/t/how-to-do-an-array-of-intlists/40894>\
**Category:** Beginners\
**Created:** [February 10, 2023, 10:18pm UTC](https://discourse.processing.org/t/how-to-do-an-array-of-intlists/40894 "2023-02-10T22:18:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rektic](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@Rektic](https://discourse.processing.org/u/Rektic)\
**Post date:** [February 10, 2023, 10:18pm UTC](https://discourse.processing.org/t/how-to-do-an-array-of-intlists/40894/1 "2023-02-10T22:18:34Z")

</div>

There seems to be a way to make an array of IntLists (I know it sounds weird but I need it) but it throws an error.

\</\>  
IntList test = new IntList[5];

void setup(){  
test[3].append(8);  
println(test[3].get(0));  
}  
\</\>

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [February 10, 2023, 10:22pm UTC](https://discourse.processing.org/t/how-to-do-an-array-of-intlists/40894/2 "2023-02-10T22:22:16Z")

</div>

> [@Rektic](#):
>
> IntList test = new IntList[5];
> 
> void setup(){  
> test[3].append(8);  
> println(test[3].get(0));  
> }

an int list is not the same type of object as an array, and is not initialized in the same manner. It more closely resembles the ArrayList format, and is initialised in the same manner. Values are added using append and other often used functions have been added for speed and elegance of code.  
initialise

```auto
ArrayList <type> n = new ArrayList<typr>();
IntList inventory = new IntList();

```

```auto
n.add(value);
inventory.append();

```

as you can see they closely resemble one another however in the case of the intList the type is already known.

> **[IntList / Reference](https://processing.org/reference/IntList.html)**
>
> Helper class for a list of int values. Lists are designed to have some features of ArrayList, but to maintain the simplicity and efficiency of working with arrays. Functions such as s…

---

<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:** [February 10, 2023, 10:27pm UTC](https://discourse.processing.org/t/how-to-do-an-array-of-intlists/40894/3 "2023-02-10T22:27:15Z")

</div>

Whenever we create an array of a non-primitive datatype, that array is by default populated w/ `null` values.

Therefore we need to repopulate such array w/ instances of that datatype before using it:

```auto
static final int LISTS = 5;

final IntList[] lists = new IntList[LISTS];

{
  for (int i = 0; i < LISTS; lists[i++] = new IntList());
}

void setup() {
  printArray(lists);
  println();

  lists[3].append(8);

  println(lists[3]);
  println(lists[3].get(0));

  exit();
}

```

---

<div class="post-metadata">

**Author:** ![Rektic](https://avatars.discourse-cdn.com/v4/letter/r/898d66/32.png) [@Rektic](https://discourse.processing.org/u/Rektic)\
**Post date:** [February 10, 2023, 10:34pm UTC](https://discourse.processing.org/t/how-to-do-an-array-of-intlists/40894/4 "2023-02-10T22:34:29Z")

</div>

Thank you very much. It seems to work!

---

<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:** [February 10, 2023, 11:09pm UTC](https://discourse.processing.org/t/how-to-do-an-array-of-intlists/40894/5 "2023-02-10T23:09:13Z")

</div>

Just for curiosity’s sake, I’ve converted my Processing Java example to Processing Jython: 🐍

```auto
from processing.data import IntList

LISTS = 5
LISTS_RANGE = range(LISTS)

lists = tuple( map(lambda _: IntList(), LISTS_RANGE) ) # map() style
lists = tuple( IntList() for _ in LISTS_RANGE ) # list comprehension style

def setup():
    print lists
    print

    lists[3].append(8)

    print lists[3]
    print lists[3].get(0)

    exit()

```

Notice that in Python we can create, populate & assign a container to a variable in 1 single statement!

We can use built-in function [**map()**](https://docs.python.org/2.7/library/functions.html#map) or a list comprehension to create a populated container.

Also notice I’m converting the result to a [**tuple()**](https://docs.python.org/2.7/library/functions.html#tuple); so we end up w/ a tuple of IntList containers; while in Java we have an array of IntList containers. ☕
