What the wrong in the code, it is just show Array memory address instead of value in first index info[0][0].
car camry = new car();
car[][]info;
info = new car[2][3];
info[0][0] = camry;
int i = 0;
while( i < info.length)
{
int j = 0;
do
{
System.out.println( info[i][j]);
j++;
}
while(j < info[0].length);
i++;
}
Another way to iterate over all the cars, and handle if there is no car there:
for(car[] cars : info)
{
for(car c : cars)
{
if (c != null)
{
// you still need to override toString() in car
// if you want to see something other than the default memory address
System.out.println(c);
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments