英文:
Array of references to objects of the class
问题
代码中的问题是,它在打印info[0][0]的值时只显示了数组的内存地址,而没有显示实际值。这可能是因为您忘记了访问camry对象的某个属性或方法来获取其值。要解决这个问题,您需要修改代码以显示camry对象的某个属性或调用方法来获取值。
以下是示例代码的修改,假设car类有一个名为getInfo()的方法来获取车辆信息:
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].getInfo()); // 修改这里以获取车辆信息
        j++;
    } while (j < info[0].length);
    i++;
}
请注意,上述示例中的getInfo()方法是一个假设的方法名称,您需要根据您的实际代码和car类的定义来替换它。这将允许您打印info[0][0]中实际的值而不是内存地址。
英文:
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++;
    }
答案1
得分: 1
另一种迭代所有汽车并处理没有汽车的方式:
for (car[] cars : info) {
    for (car c : cars) {
        if (c != null) {
            // 如果你想看到除了默认内存地址之外的内容,仍然需要重写car类中的toString()方法
            System.out.println(c);
        }
    }
}
英文:
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);
        }
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论