• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Multi-d array

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1int [] [] x = new int [2] [];

2x[0] = new int [2];
3x[0][0]=3;
4x[0][1]=7;

tells me ,
5line 1 makes a base array with elements -->0,1<--
6line 2 makes 2nd dimension array, orginating from the '0' element [line 5 in this mail ]
and the 2nd dimension array gets filled with values 3 and 7.

[I'm not going into the 2nd element of the base array, i.e line 1 ]


Now completely detaching myself from the above example , how shall I interpret something like :-

int [][] x = new int[2][2]; //...element wise

TIA
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me change your example just a little so that it's easier to discuss:



By making the dimensions different, it's a little easier to ensure we're all on the same page.

In this case, you've created an array with 2 elements. You can access those elements using x[0] or x[1]. Each of those elements contains an array of 3 elements. To get at the first element of each of those arrays, you could use x[0][0] or x[1][0].

I hope that helps.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int [][] x = new int[2][2];
Here x is a two dimensional array. x can hold 2 arrays of length 2 in the positions x[0] and x[1].

x[0] can hold an int array of length 2.
x[1] can hold an int array of length 2.
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent stuff...!
naggy doubt cleared here !

and now back to the books
 
Netty poestel
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for being back again so soon, after signing off so cool-ly


and the command-line invocation,
java CommandArgsThree 1 2 3
what is the result?

I'm not being able to interpret this code.
from intial feedbacks this is how I'm seeing the picture

____________
| 0 | 1 |
|____|_____|

____________
| 0 | 1 |
|____|_____|




____________
| 0 | 1 |
|____|_____|
argcopy


each element in "argcopy" contains an array of 2 elements as shown
above 'argcopy' [ could not draw the linking lines....too painful ]
considering x = argCopy[0].length; should be 3 , what is happening with

System.out.print(" " + argCopy[0][y]); // in terms of the boxes I have
drawn
reply
    Bookmark Topic Watch Topic
  • New Topic