permaculture playing cards*
The moose likes Beginning Java and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

Jugal Hans
Greenhorn

Joined: Aug 30, 2004
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

Joined: Aug 03, 2002
Posts: 7729
"XYZ ABC" please change your displayed name to conform with our JavaRanch Naming Policy.

You can change it here.

Thanks


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jugal Hans
Greenhorn

Joined: Aug 30, 2004
Posts: 17
Done.......
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
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.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Jugal Hans
Greenhorn

Joined: Aug 30, 2004
Posts: 17
Thanks for the reply.
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

Joined: Jul 30, 2003
Posts: 804
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

Joined: Aug 30, 2004
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays
 
Similar Threads
Spiral Matrix
want to found out some things
Storing user input into 2-d Array
Dimensional Travel
Pass 2D array between methods