aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Array Declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Array Declaration" Watch "Doubt in Array Declaration" New topic
Author

Doubt in Array Declaration

Sriram.Narayanan Thiagarajan
Greenhorn

Joined: Jul 21, 2006
Posts: 26
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
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

Joined: Aug 03, 2002
Posts: 7729
Never write array declarations like that - nobody understands them.
Sriram.Narayanan Thiagarajan
Greenhorn

Joined: Jul 21, 2006
Posts: 26
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

Joined: May 30, 2004
Posts: 78
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

Joined: Jul 21, 2006
Posts: 26
I am preparing for SCJP1.4

when writing an mock exam , I came across this type of question
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Array Declaration
 
Similar Threads
Operators
Ternary operator problem
Evaluation order
Conversion.....
array