Win a copy of Functional Reactive Programming this week in the Other Languages forum!

Arrays

Jugal Hans
Greenhorn
Posts: 17
Hello,
I am kind of new to java programming. Although I have programming experience but not on open systems like java,C etc..
I am trying to learn java and got confused with 2D arrays. Can anyone suggest some informative site for a detailed explaination on this topic. Somwhow this concept of 2D array is not fitting in my head.
Help!! Help!!

Barry Gaunt
Ranch Hand
Posts: 7729
"XYZ ABC" please change your displayed name to conform with our JavaRanch Naming Policy.

You can change it here.

Thanks

Jugal Hans
Greenhorn
Posts: 17
Done.......

Jeff Bosch
Ranch Hand
Posts: 805
Hi, Jugal -

Sun has a free downloadable extensive Java tutorial, which includes discussions of arrays. Here's a quickie overview:

Think of a two dimensional array as a set of boxes. For example, if you have an array that is 3x2, you would have three big boxes each holding two little boxes, and each little box stores data of whatever type the array was declared to hold. If you have a 3x5x8 array, you have three big boxes, each holding five medium boxes, each holding eight small boxes and each little box... you get the idea.

Jugal Hans
Greenhorn
Posts: 17
That is how I also visualize conceptually.
So for eg:
If the array is defined as Arr[2][3],
then if arr[2][] is interrogated then it should show data containing
in arr[2][1] and arr[2][2] and arr[2][3]...., but it does not ? Can u tell me more in detail how does the data reside ?

Jeff Bosch
Ranch Hand
Posts: 805
then if arr[2][] is interrogated then it should show data containing in arr[2][1] and arr[2][2] and arr[2][3]...., but it does not

Actually, there is no data in arr[2][], only another array. You can only check data when you fully dereference the array, and then only if you properly initialized the elements of the array. Just declaring an array does not initialize it with data. (See Java's initialization default values for the data type of the array.)

Also, in Java, the second-level arrays do not have to be the same size, so arr[2][] could hold a three-element array, while arr[1][] could hold a twelve element array.

Jugal Hans
Greenhorn
Posts: 17
Ok,
I understand that arrays need to be initialized explicitly
So assuming the array is initialized with proper values and as per ur answer Arr[2][] contains only arrays again then why does Arr[2][] show junk values? What is it that it is showing?

You can only check data when you fully dereference the array

What do you mean by the above statement ? I did not get a bit of it...
and I think my answer is hidden in the quote above.
[ November 17, 2004: Message edited by: Jugal Hans ]