Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Issue

 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I need a favour from you.

I saw the following code in Inquisition John Meyers' exam



My answer is this print null, but the answer in the system is compile error, b is a two dimension array a has one dimension? I need your explaination.

I actually test this in eclipse, the result just like I said print null.
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int a [] = null, b [] = null;


we can understand this as
int a[]=null;
int[]b[]=null ;

since a two dimensional array can be written as
int[][]b or int[]b[] or int b[][] all these declarations are valid .Also,

we can't make reference of two dimensional array to point one dimensional array,as both are totally diffent object. hence we are getting compilation error at line

[ November 07, 2008: Message edited by: Prabhat Gupta ]
 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat,

Thanks for your answer, I understand your point, but this puzzled me because when I tested in Eclipse, it does not return any errors and prints null. I guess the compiler of my Eclipse indeed treats this code separately as two one dimension array.
[ November 07, 2008: Message edited by: Henry Zhi Lin ]
 
M Srilatha
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following code willnot compile because we are trying to assign one dimension array to 2 dimension array! (here [] is placed next to "int")


But in the code which is posted above: (here [] is placed next to variable name)

There is nothing wrong. It will compile and give null as output.

@Prabhat: Could you please explain why you have considered the array b as 2 -dimension array here?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no such thing in Java as a 2-dimensional array, but there is such a thing as an array of arrays, eg int[][]. That is an array of arrays.
There is a way of creating an array and an array of arrays in the same statement

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

That is probably what you thought you wrote, and in combination with the rest of your code will produce an error.

By the way: I would regard that bit of code as obscure and hard to read and bad style.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M SRILATHA could you please adjust your display name so that it does not contain all upper case letters, like M Srilatha.

Thanks
 
Henry Zhi Lin
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
M SRILATHA,

You are right, I checked again, just as you said, I put the [] in wrong place. Thanks for pointing out for me.
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Srilatha M" please check your private messages for an important administrative matter.

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