• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two dimensional Array (Strange thing)

 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int a[] = null, b[] = null;

a=b; // 1, this line produces compile time error, with message can't assign two dimensional array 'b' to single dimensional array 'a'.

Q) how can be 'b' is an two dimensional array. i didn't write b[][], its simply b[].
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code compiles fine for me on java 5
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you didn't write
That would declare a as a 1D array and b as a 2D array.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, very clever!
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't understand what you are trying to say.

This code does not compile on my machine, its a question from one mock exam of scjp
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles fine on mine. Are you sure you copied it properly. Post the code you are testing it with.
Notice that I moved the [] that were after a to before it
i.e.
int a[]
became
int[] a
[ August 20, 2008: Message edited by: Joanne Neal ]
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actuall code is this.

class Testing {

public static void main(String args[]) {

int[] a = null, b[]=null;
b=a;
System.out.println(b);
}
}
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by VijayGupta gupta:
Actuall code is this.

class Testing {

public static void main(String args[]) {

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


Compare that to what you actually typed in your original post. Note the position of the two []s
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, my mistake.

But why this error comes of incompatible types.
found : int[]
required : int[][]
b=a;


q) Is'nt b is single dimensional array, please explain why this error come, still I didn't get this thing.
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how it effects changing the position of []

from int [] a = null, b[] = null to this
int a[]=null,b[] = null
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The recent discussion ll solve your problem !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic