A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
RabbitMQ in Depth
this week in the
Programmer Certification (SCJP/OCPJP)
Assigning array references
Joined: Sep 20, 2005
Mar 14, 2006 13:59:00
So... reading the K+B book.
If you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to.
so why does this compile corectly?
int  array1; // here is an array reference int  array2 = new int ; array1 = array2; // here is an array reference with a different dimension: int  array3 = new int ; // here is me assigning the array reference with a different dimension to an the previously declared arrayed reference. array2 = array3; // and just for fun, doing the same thing explicitly. array2 = new int;
What am I doing wrong here?
Joined: Aug 03, 2002
Mar 14, 2006 14:03:00
Your arrays are of a different size, not of a different dimension. They are all one dimensional int arrays. The dimension of an array is the number of indexes required to locate an element.
Ask a Meaningful Question
Getting someone to think and try something out is much more useful than just telling them the answer.
Joined: Jan 17, 2006
Mar 14, 2006 19:42:00
"dimensions" refers to the number of sets of brackets the array has, to put it simply.
int oneDimensional = new int[x][y]; //illegal
I agree. Here's the link:
subject: Assigning array references
About Array reference assignments for One-D arrays
Question on similar & identical of Objects
arrays - different size
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015