# [chuck-users] .cap() for multi-dimensional arrays

Spencer Salazar ssalazar at CS.Princeton.EDU
Wed Oct 3 04:18:06 EDT 2007

```It makes sense when you think of multi dimensional arrays as just
arrays of arrays.  Additionally each sub-array need not be of equal
length.  Try running this code for example:

int a[4][5];

<<< "here", "" >>>;

for( 0 => int i; i < a.cap(); i++ )
<<< a[i].cap(), "" >>>;

<<< "there", "" >>>;

int b[4] @=> a[1];

for( 0 => int i; i < a.cap(); i++ )
<<< a[i].cap(), "" >>>;

In this case if you relied on a[0].cap() specifying the length for
each sub-array you could get yourself a NullPointerException trying
to access a[0][4].

spencer

On Oct 1, 2007, at 2:49 AM, Kassen wrote:

>
>
> On 10/1/07, Martin Ahnelöv <operagasten at gmail.com> wrote:
>
>
>
> Eh? What are you... Hm... Okay, I can see your point now. You're
> right.
> Since we are dealing with arrays, every array inside foo[i] will be of
> equal length; it's not like we are dealing with lists where every slot
> is just a reference to another object.
>
> Good thinking there.
>
>
> It gets very useful. Suppose we have two positive integers X and Y
> in a file so that;
> -----------------------------------------------------------
> array[X][Y] @=> Foo.foo_array;
> Foo foo;
>
> public class Foo
>   {
> static int foo_array[][];
>
> fun void bar(int blah)
>     {
>     ................
>     }
>   }
> ----------------------------------------------------------
>
> Because Foo is a public class holding a static array the array
> needs to be instantiated outside of the class or there will be
> errors. Now if Foo.bar() does something to that array it becomes
> quite important to determine what it's dimensions are, however,
> both X and Y are outside of Foo's namespace and it's quite possible
> that there will be many instances of Foo that are in different
> files altogether which won't have X or Y.
>
> At that point the .cap()'s of multi-dimensional arrays become very
> interesting so I thought I'd share because it's not in the manual
> nor does my workaround look very nice. Also; this took a while to
> figure out and it makes no sense for others to spend that time too.
>
> Yours,
> Kas.
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users

```