How to do an array of IntLists?

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));
}
</>

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

ArrayList <type> n = new ArrayList<typr>();
IntList inventory = new IntList();
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.

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:

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();
}
3 Likes

Thank you very much. It seems to work!

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

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() or a list comprehension to create a populated container.

Also notice I’m converting the result to a tuple(); so we end up w/ a tuple of IntList containers; while in Java we have an array of IntList containers. :coffee:

1 Like