• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Arrays

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done.......
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic