Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes Two dimensional Array (Strange thing) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Two dimensional Array (Strange thing)" Watch "Two dimensional Array (Strange thing)" New topic
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
    
    9
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
    
    9
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
    
    9
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
    
    1

The recent discussion ll solve your problem !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Two dimensional Array (Strange thing)
 
Similar Threads
Your inputs
array doubt
Array Issue
Explain this code about Arrays
One more Confusing Output