How do I make "Stacked" arrays (solved)

edited April 2014 in How To...

I have some programing experience. Lately I have used Ruby. In Ruby it is possible to "stack" arrays. I am trying to do a similar thing in processing and I can't figure out how. A "stacked" array in an array within an array. Here is an example.

array[1, 4,[6,5,0],3,7] array[1] returns 1 array[2] returns 4 array[3,1] returns 6 array[3,2] returns 5 etc.

As I mentioned this works in Ruby, and I would like to do this in processing. Any help?

Answers

  • edited April 2014 Answer ✓

    We can't do that in a direct form w/ Java's arrays! B/c all elements within 1 gotta be of the same data type. 8-X
    Let's say we wish to store byte type values. We can't have both byte & byte[] inside the same array! #-o

    However, rather than having a mix of byte & byte[] elements, let's make all of them byte[]! =:)
    Therefore, a former byte becomes a byte[] of length = 1 element now! *-:)
    And we end up w/ a 2D byte[][] array instead of 1D! :ar!

    //forum.processing.org/two/discussion/4808/how-do-i-make-stacked-arrays  
    
    final byte[][] stacks = {
      {1}, {4}, {6, 5, 0}, {3}, {7}
    };
    
    int i = 0;
    for (byte[] b: stacks) {
      println(i++);
      println(b);
      println();
    }
    
    exit();
    
  • edited April 2014

    Thanks, just a few questions, Final byte [][] stacks = {{1},{2,1}}; Is how I define the array right? If I wanted to call a specific element I could say byte[2][1] right?

  • Answer ✓

    To declare an array variable we use data-type[]. Replace data-type w/ the chosen type.
    There are a couple of ways to instantiate an array object. I've used {} in my example above.

    http://processing.org/reference/Array.html
    http://processing.org/reference/arrayaccess.html
    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Sign In or Register to comment.