• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Doubt in Array Declaration

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never write array declarations like that - nobody understands them.
 
Sriram.Narayanan Thiagarajan
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
There's a hole in the bucket, dear Liza, dear Liza, a hole in the bucket, dear liza, a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic