This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please explain me Array Reference Assignments for Multidimensional Array

 
Ami Ambre
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cat [][]my= {{new cat("F"),new cat("Z")},
{new cat("B"),new cat("R"),new cat("L")}};

cat []c=new cat[];

my=my[0];
//can't assign a 1-D array to a 2-D array.

my=my[0][0];
//can't assign a nonarray object to a 2-D array reference.

my[1]=my[1][2];
//can't assign a nonarray object to a 1-D array reference.

my[0][1]=c;
//can't assign an array object to a nonarray reference my[0][1] can only refer to a cat object
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


cat []c=new cat[]; // this won't work, you have to specify size of the instantiated array, e.g. new cat[5];

my=my[0];
//can't assign a 1-D array to a 2-D array.


my=my[0][0];
//can't assign a nonarray object to a 2-D array reference.

my[1]=my[1][2];
//can't assign a nonarray object to a 1-D array reference.

my[0][1]=c;
//can't assign an array object to a nonarray reference my[0][1] can only refer to a cat object[/QB]


What specifically you don't understand? There is one simple rule here: n-dimensional array reference of object x can only refer to instance of exactly n-dimensional array of object x (or subclass of x, since arrays are polymorphic) - period. In your case, cat[][] instance cannot be assigned to cat[] reference and vice versa, and so on...
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. It looks like the comments does a very good job explaining everything. What is it that is not understood?

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic