Array list of Arrays ( Syntax Issue)

edited November 2017 in Questions about Code

Hey guys,

Been banging my head over this ( new to processing ), cant quite seem to figure it out.. I've made an array list and I'm trying to populate a bunch of arrays in setup() with unique id's using a "for" loop, just can't seem to work out the syntax any help would be appreciated. Cheers! .. Snippet of a larger code below..

int res_x = 10; // This will eventually be a dynamic number

ArrayList<Array> SX;

void setup(){

SX = new ArrayList<Array>();

// here I want to iterate through and create a bunch of arrays and add them to my array list!

for (int i = 0; i < res_x; 1++){
  int[] vert_x+"i" = new int[res_x]; // this is the line I'am having trouble with!
  SX.add(vert_x+"i");
}

println(SX.size());
}

Any help would be awesome!

Tagged:

Answers

  • Do you have a class called Array? Probably not. Think about how you declare an array. You give it a type and use the [] syntax, like this:

    int[] array = new int[10];

    So to create an ArrayList that holds an array, you simply pass the type of the array into the <> part, which is called a generic type argument. Like this:

    ArrayList<int[]> list = new ArrayList<int[]>();

    Now to add an array to your ArrayList, you just combine the ideas:

      //create the array
      int[] array = new int[10];
    
      //add stuff to the array
      array[0] = 17;
    
      //add the array to the ArrayList
      list.add(array);
    
  • edited June 2014 Answer ✓

    referencing those is a case of

    int[] firstArray = list.get(0);
    int firstInt = firstArray[0];  // first element of first array
    

    (oops, hit accept instead of edit...)

  • how would you create an array that holds and array list?

  • edited November 2017

    An array of ArrayLists:

    ArrayList[] arraylists = new ArrayList[3];
    arraylists[0] = new ArrayList();
    arraylists[0].add( new Whatever() );
    
  • You could also make an ArrayList of arrays of ArrayLists...

    or an array of ArrayLists of arrays of Arraylists of arrays...

    However at a certain point retrieving and updating data gets so complex and painful to write that you should consider an easier interface -- for example, using Table, or creating a custom class.

Sign In or Register to comment.