• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array dimension confusion

 
pawni jain
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello to all javaranchers ,,

My doubt is simple i think...but i got confused about array dimensions.
Please see at following code..

int[] a = null , b [] = null;
b = a;
System.out.println( b );

output:
compiler error
incompatible type..


compiler take b as 2-d array and a as 1-d array.
but b is also 1-d array here..how come b is 2-d array.

please do reply.

Thanks.
With Regards.
 
Henry Wong
author
Marshal
Pie
Posts: 21009
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler take b as 2-d array and a as 1-d array.
but b is also 1-d array here..how come b is 2-d array.


Maybe, because b *is* a 2D array? (ie. an array of arrays)

Take this code....



You can actually separate this to two separate lines...



Does it look more like a 2D array now?

Henry
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please quote the source of your questions. This appears to have come from my exam

It works like this. When you place the [] before a variable name the compiler treats the next element in the declaration as already having a dimension. Which is to say

int[] a = null , b [] = null;

a - one dimension b - 2 dimension

int a[] = null , b [] = null;

a - one dimension b - one dimension
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John, I did not get your point. Can you please explain with little more details.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dean Jones:
Hi John, I did not get your point. Can you please explain with little more details.


What part of the declaration is confusing to you ? As Henry pointed out the declaration can actually be split into 2 lines. If you notice my illustration you will find that the position of the [] characters differs for the first and second example
 
pawni jain
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Thanks henry and john..i got the idea behind the code.

Regards.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic