• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with compiling?

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am not getting the logic why is it the number 7 not being assigned to the 0 index?
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
books[0] is declared to hold an int or an int array?
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:books[0] is declared to hold an int or an int array?


I am assigning a numerical value to an index 0 , isnt it right?
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
books is a multidimensional array that holds integer arrays. So book[0] - is a place holder for an integer array. book[1] for another and book[2] another. The no. of integer arrays that book can hold is declared with the size 3.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so what does the 2 empty square bracket specify in this scenario?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:Ok, so what does the 2 empty square bracket specify in this scenario?

Indicates that books is an array of arrays (multidimensional array)
so books[0], books[1], books[2] would be 3 arrays each of whose length can be anything (as you havent mentioned the 2nd dimension value in the declaration. Do try out a full fledged program and see how it behaves.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote: . . . multidimensional array . . .
Careful; the newbies will believe there actually is such a thing as a multidimensional array!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:

I am not getting the logic why is it the number 7 not being assigned to the 0 index?


Because anumber is not the same as aNumber. But I'm guessing that's not really the problem. This is why you should copy/paste code, not type it fresh here.

Assuming you have the correct variable name so that you're not getting a symbol not found error, you're instead getting this error message. Read it closely. What do you think it means?




You know that X[] arr; means that each element of arr is an X, right? Well, if you have int[][] arr;, then what is X here? And therefore what type is each element of arr?
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Vishal Hegde wrote:

I am not getting the logic why is it the number 7 not being assigned to the 0 index?


Because anumber is not the same as aNumber. But I'm guessing that's not really the problem. This is why you should copy/paste code, not type it fresh here.

Assuming you have the correct variable name so that you're not getting a symbol not found error, you're instead getting this error message. Read it closely. What do you think it means?




You know that X[] arr; means that each element of arr is an X, right? Well, if you have int[][] arr;, then what is X here? And therefore what type is each element of arr?


Hi Jeff,

int[][] in here x is an int array and the type of element will be having an int array :O

ok if incase there are two [][] then it means it takes int array, but what if there are three square brackets [][][] what in that case ??
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:ok if incase there are two [][] then it means it takes int array, but what if there are three square brackets [][][] what in that case ??

Then it's an array of array of arrays. If you declare numbers as an int[][][], then numbers[0] is an array-of-arrays-of-ints, numbers[0][0] is an array-of-ints, and numbers[0][0][0] is an int.

The thing to remember is that arrays are just objects. You can have an array of any object you want, including other arrays. And arrays of those, and of those, etc. There's no absolute restriction on how many levels you can nest it as, but as you can imagine it gets difficult to follow so it's not a good idea to take it too far.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Hegde wrote:ok if incase there are two [][] then it means it takes int array, but what if there are three square brackets [][][] what in that case ??

myarray[x] = <array of int arrays>
myarray[x][y] = <array of ints>
myarray[x][y][z] = <int>
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , thanks a lot My doubts are getting cleared now
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote: . . . There's no absolute restriction on how many levels you can nest it as, . . .
[Pantomime mode]Oh, yes, there is![/Pantomime mode]
JVM Specification §4.10.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Matthew Brown wrote: . . . There's no absolute restriction on how many levels you can nest it as, . . .
[Pantomime mode]Oh, yes, there is![/Pantomime mode]
JVM Specification §4.10.


The number of dimensions in an array is limited to 255

So if it had, say, 3 dimensions, wouldn't that make it a multidimensional array




Edit: Note to newbies. I'm just winding Campbell up here. He is right when he says (over and over again) that there is no such thing as a multidimensional array in Java
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means they are using language loosely, since it says elsewhere that a mmmmm-[I can’t say it]-dimensional array is an array of arrays.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic