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

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:
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic