“Array of references to objects of the class” 可翻译为: “对该类对象的引用数组”。

huangapple go评论64阅读模式
英文:

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 &lt; info.length)
    {
        int j = 0;
      do 
      {
          
        
        System.out.println( info[i][j]);
        j++;
      }
      while(j &lt; 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);
        }
    }
}

huangapple
  • 本文由 发表于 2020年7月28日 05:43:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63124026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定