File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Array Declaration

 
Sriram.Narayanan Thiagarajan
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int []a,b[] ; // LINE 1
a = new int[3];
b=a;


on compiling the piece of code , Iam getting the following error

found : int[]
required: int[][]
b=a;
^
can anybody explain ,how the compiler reads LINE 1
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It read line 1 as


So when you assign b = a; you are trying to assign a one-dimensional array reference to a two-dimensional array reference.
[ July 28, 2006: Message edited by: Barry Gaunt ]
 
Gowher Naik
Ranch Hand
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a is single dimensional array
b is double dimensional array
and u are refering single dimensional array to double dimensional array
which is compile time error.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write array declarations like that - nobody understands them.
 
Sriram.Narayanan Thiagarajan
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int []a,[]b ; // LINE 1

a = new int[3];
b=a;


If I change line 1 ,its giving complier error

<identifier> expected
int []a,[]b ; // LINE 1
 
Andy Morris
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the initial declaration for the arrays I don't understand how array a is 1 dimensional and b is 2 dimensional. I'd assumed they are both 1 dimensional.

I'd agree that you wouldn't want people to declare arrays like that because no-one understands them, which is kind of my question, as I'd just like to understand how that declaration is working.
[ July 28, 2006: Message edited by: Andy Morris ]
 
Sriram.Narayanan Thiagarajan
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am preparing for SCJP1.4

when writing an mock exam , I came across this type of question
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andy Morris:
Looking at the initial declaration for the arrays I don't understand how array a is 1 dimensional and b is 2 dimensional. I'd assumed they are both 1 dimensional.

I'd agree that you wouldn't want people to declare arrays like that because no-one understands them, which is kind of my question, as I'd just like to understand how that declaration is working.

[ July 28, 2006: Message edited by: Andy Morris ]


Try:
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic