> Hi, if in Java we can create a triangular array like in the following
> listing:
[quoted text clipped - 9 lines]
>
> How, work the compiler behind the scenes?
I don't know what you mean by "assimilable" in this context, but arrays
in Java are a kind of object. In the above code, "a" is reference pointing
to an object of type "int[][]". "a[0]" is an expression which resolves to a
reference pointing to an object of type "int[]". There is no requirement in
Java that objects take up a contiguous region of memory.
- Oliver
> int a[][] = new int[2][];
> a[0] = new int[3]; // 3 cols
> a[1] = new int[4]; // 4 cols
>
> can we assert that it's assimilable at the C array of pointers?
If you built a triangular array in C, using the "natural" representation as an
array of pointers, then it would be quite similar to the above, yes.
> and a non-triangular array like a matrix is assimilable at the C
> pointer to array?
Depends. A rectangular array in C can be constructed in two ways. One way is
just like the triangular array, where the "main" array contains pointers to
separate arrays for each row (or is it column -- I can never remember ?) The
other way is that the C compiler will build a single block of memory of size
N*M, and each row/column access will be converted by the compiler into a single
access to the correct cell in that array (using a bit of arithmetic to help).
The second form has the advantage that it doesn't require a double indirection
to get at the data.
If you create a two dimensional array in Java, then you are using the
equivalent of the first form; there is no built-in equivalent to the second.
So if you need the extra performance (which sometimes happens, but not often)
then you have to use a single large array and code up the arithmetic yourself.
-- chris
josh - 26 Oct 2006 15:06 GMT
Chris Uppal ha scritto:
> Depends. A rectangular array in C can be constructed in two ways. One way is
> just like the triangular array, where the "main" array contains pointers to
> separate arrays for each row (or is it column -- I can never remember ?)
in an array of pointers there is one row and the colums numbers are the
pointers we're
referring to
> If you create a two dimensional array in Java, then you are using the
> equivalent of the first form; there is no built-in equivalent to the second.
> So if you need the extra performance (which sometimes happens, but not often)
> then you have to use a single large array and code up the arithmetic yourself.
how can in Java code up the arithmetic myself if I can't access the
real address of the array?
Oliver Wong - 26 Oct 2006 15:30 GMT
> Chris Uppal ha scritto:
>
[quoted text clipped - 8 lines]
> how can in Java code up the arithmetic myself if I can't access the
> real address of the array?
You'd write a function which maps from one coordinate space to the
other.
second
index
|0 1 2 3
-+-------
first 0|0 1 2
index 1|3 4 5 6
So you'd write a function which, given (0,0), returns 0, and given (1,2)
returns 5, for example.
- Oliver
Bart - 26 Oct 2006 18:18 GMT
> > If you create a two dimensional array in Java, then you are using the
> > equivalent of the first form; there is no built-in equivalent to the second.
[quoted text clipped - 3 lines]
> how can in Java code up the arithmetic myself if I can't access the
> real address of the array?
array[row * numCols + col] = value;
Regards,
Bart.