How can i make an n-dimensional array in processing?

edited November 2013 in How To...

This for example is a two dimensional boolean array:

boolean[][] a;

So how can i make an n dimensional array:

boolean[][][]..n-times[] a;

Thanks

Tagged:

Answers

  • edited November 2013

    The # of [] is the # of dimensions, isn't it? 3:-O

  • Answer ✓

    The number of dimensions is determined at compilation time, it is not dynamic, if that's what you mean... Now, you can create dynamic structures, but a real use case would be needed to answer more precisely.

  • edited November 2013

    Well i don't understand the answer, so for example i have an integer b that declares the length of the dimensions of a, how would i declare it.

    int b = 45; //b is unknown this is an example
    boolean[][][][][]..btimes[][][];
    

    So you theoretically would write it like this: boolean[][][][][][][][][][][][][][][][][][][][][][][][][] a;

    Btw thanks for the support!

    EDIT: I don't need to make it dynamic, you could also use concat() on an array, but how can i declare the dimensions of an array.

  • Again, why do you need this? If it is theoretical, it is a bit pointless. I see no real usage of your question...

    Basically, my answer above means it is not possible in Java.

  • edited November 2013

    I actually need it to solve a dynamic programming issue.

    In C++ i think you can solve it with vectors. If there's any way with using array list or whatever i'd be glad to know the answer. Help's very appreciated.

    EDIT: I'm going to accept the answer now, but if you have another solution please tell.

  • Indeed, using array lists comes to mind. You can put anything in them, including arrays or other array lists.

    But a more detailed description of your need would allow us to give a more precise answer.

  • Answer ✓

    There is no reason to have a n-dimensional array for any problem. Just as a 2-dimensional array can be mapped to a 1-dimensional array, a n-dimensional array can be mapped to a 1-dimensional array. The syntax alone would be a nightmare not to mention the problem of indexing a n-dimensional array.

    Additionally (as you guys pointed out) you could use vectors / ArrayLists if changing the size of the structure mattered.

  • edited November 2013

    Ok you have an input integer array which holds numbers, for example: int[] example = {2,4,5,6};

    Now i want to pick one number from one of the sides ( 2 or 6). After that i want to take another one from both of the sides and so on (You see the amount is increasing exponentially). Each time you add the numbers you take from the number and then subtract the next number. For example i first take +2 and then i take -4 and then i take +6 and then i take -5.

    If this tree is getting bigger you can't simply go for backtracking a trial and error method, so we use dynamic programming by saving certain paths. To save them i need this n-dimensional array :)

    EDIT: of course asimes, i wasn't thinking straightly XD But if there's a command that could help that would be fine. I forgot its actually saved as tables and how it's saved :P I'll mark your answer as accepted though if theres an object made for this reasons please tell. Thanks o/ Helped a bunch

  • edited November 2013

    I'm guessing that you need the array for memoizing the recursion: http://en.wikipedia.org/wiki/Memoization

    You would need an array of size n+1 (where n is the input size now, not the dimensions) and it would take O(n) time.

Sign In or Register to comment.