• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with arrays of arrays  RSS feed

 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i'am very new to here..
and also i 'll hope this forum help me to improve my java knowledge..

so this is my 1st question in here. hope you 'll help to me get answer to this problem..

class Ab{

int[][] myArray = new int[3][];
myArray [0] = 7;
//myArray [0] [1] = 7;
//myArray [0] [2] = 3;
//myArray [1] = new String[2];
}


when i run this program i get 4 compile time errors. i can't find what is the reason for that. can anyone help me to solve this problem..


E:\OCJP Practicals\BookExample\Assignment\New folder (10)>javac Ab.java
Ab.java:4: error: ']' expected
myArray [0] = 7;
^
Ab.java:4: error: ';' expected
myArray [0] = 7;
^
Ab.java:4: error: illegal start of type
myArray [0] = 7;
^
Ab.java:4: error: <identifier> expected
myArray [0] = 7;
^
4 errors
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to JavaRanch!
The problem in your code is this line:

The syntax of this line isn't valid, because myArray is a nested array, and should be accessed as such. Also, its location within the class is not valid; you'll need to move it into a constructor, initilization block or method.
By the way, as you may have noticed, I've added syntax highlighting and line numbering to my Java code fragment, which makes it easier to read. You can do this too, all you have to do is UseCodeTags (<-click)
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Thank you very much Jelle Klap... hmm i may think there is a no such a way mention you in here..

after your reply... i put my code into initialization block.. but it's again it's give me a compile error, because of the this line



i think the problem is, this is a multidimensional array, so i can't put a element in this way. am i right?.. so i change my code in this way.



so now problem is ok.. because of you.. thank you again.

now i have another problem related to the same question

in your reply you use a word "nested array" what does it mean?.. i think you mean "multidimensional array" am i correct?.. am i wrong please correct me.

and also i know i can't put an element in this way to my "multidimensional array"



but,,, i can put another int [] object to my multidimensional array in this way



i think you can understand my problem. so i need to know what is the reason for that. please help me to get the reason for that..
actually i hope to seat OCPJP exam in next month.. so every problem i put in this forum i need clear reason for that problem that is why i always ask you give me a reason, hmm don't misunderstand me.

thank you...
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randika Isuru wrote:in your reply you use a word "nested array" what does it mean?.. i think you mean "multidimensional array" am i correct?.. am i wrong please correct me.

Java doesn't have true multidimensional arrays. What it has is the ability to put arrays inside other arrays - nested arrays. So when you define a "multidimensional array" like this: int[][] myArray you aren't defining one array that has two dimensions, you are defining one array that holds int arrays (i.e. an int[] array, or int[][]). This is noticeable in a couple of ways:
1) each 'row' in the int[][] can be a different size
2) there is no single index access (you can't do myArray[1,2] you need to do myArray[1][2])
3) You have to initialize the outer array, and each inner array separately (no int[][] myArray = new int[10][10])
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Steve Luke. now i got the reason why it's called "arrays of arrays" because one array holds another array..
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always think of eggs when talking about arrays. Ultimately, you want an easy way move around a bunch of eggs. So, you develop a carton that can hold 12, or 18, or whatever. But an egg carton can only hold eggs.

But now, I want a way to move a bunch of egg cartons around. So, I make a crate that is designed to hold egg cartons. The cartons inside could be different sizes, or be only partially full (or even empty). That is an array of array - a crate that hold cartons that holds eggs.

You can extend this up into higher dimensions...

A palate has a bunch of crates on it.

A truck has a bunch of palates on it.

A shipping company has a bunch of trucks...

etc.

ULTIMATELY, at the very end, I've got eggs...but they are in something held by something that is held by something...all the way up.
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you fred rosenberger...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!