Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Two dimensional Array (Strange thing)

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[].
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code compiles fine for me on java 5
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha, very clever!
 
V K Gupta
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The recent discussion ll solve your problem !
 
I've never won anything before. Not even a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic