| Author |
Two dimensional Array (Strange thing)
|
V K Gupta
Ranch Hand
Joined: Aug 07, 2008
Posts: 52
|
|
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
Joined: May 11, 2005
Posts: 142
|
|
|
That code compiles fine for me on java 5
|
<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Are you sure you didn't write That would declare a as a 1D array and b as a 2D array.
|
Joanne
|
 |
Tom Johnson
Ranch Hand
Joined: May 11, 2005
Posts: 142
|
|
|
Aha, very clever!
|
 |
V K Gupta
Ranch Hand
Joined: Aug 07, 2008
Posts: 52
|
|
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
Joined: Aug 05, 2005
Posts: 3011
|
|
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
Joined: Aug 07, 2008
Posts: 52
|
|
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
Joined: Aug 05, 2005
Posts: 3011
|
|
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
Joined: Aug 07, 2008
Posts: 52
|
|
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
Joined: Aug 07, 2008
Posts: 52
|
|
how it effects changing the position of [] from int [] a = null, b[] = null to this int a[]=null,b[] = null
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
|
The recent discussion ll solve your problem !
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
 |
|
|
subject: Two dimensional Array (Strange thing)
|
|
|