Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

triangular array and matrix in Java VS C

Thread view: 
josh - 25 Oct 2006 14:37 GMT
Hi, if in Java we can create a triangular array like in the following
listing:

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?

and a non-triangular array like a matrix is assimilable at the C
pointer to array?

How, work the compiler behind the scenes?

Thanks
Oliver Wong - 25 Oct 2006 19:14 GMT
> 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
Chris Uppal - 26 Oct 2006 11:48 GMT
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.