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

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

Array of references to objects of the class

问题

代码中的问题是,它在打印info[0][0]的值时只显示了数组的内存地址,而没有显示实际值。这可能是因为您忘记了访问camry对象的某个属性或方法来获取其值。要解决这个问题,您需要修改代码以显示camry对象的某个属性或调用方法来获取值。

以下是示例代码的修改,假设car类有一个名为getInfo()的方法来获取车辆信息:

  1. car camry = new car();
  2. car[][] info;
  3. info = new car[2][3];
  4. info[0][0] = camry;
  5. int i = 0;
  6. while (i < info.length) {
  7. int j = 0;
  8. do {
  9. System.out.println(info[i][j].getInfo()); // 修改这里以获取车辆信息
  10. j++;
  11. } while (j < info[0].length);
  12. i++;
  13. }

请注意,上述示例中的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].

  1. car camry = new car();
  2. car[][]info;
  3. info = new car[2][3];
  4. info[0][0] = camry;
  5. int i = 0;
  6. while( i &lt; info.length)
  7. {
  8. int j = 0;
  9. do
  10. {
  11. System.out.println( info[i][j]);
  12. j++;
  13. }
  14. while(j &lt; info[0].length);
  15. i++;
  16. }

答案1

得分: 1

另一种迭代所有汽车并处理没有汽车的方式:

  1. for (car[] cars : info) {
  2. for (car c : cars) {
  3. if (c != null) {
  4. // 如果你想看到除了默认内存地址之外的内容,仍然需要重写car类中的toString()方法
  5. System.out.println(c);
  6. }
  7. }
  8. }
英文:

Another way to iterate over all the cars, and handle if there is no car there:

  1. for(car[] cars : info)
  2. {
  3. for(car c : cars)
  4. {
  5. if (c != null)
  6. {
  7. // you still need to override toString() in car
  8. // if you want to see something other than the default memory address
  9. System.out.println(c);
  10. }
  11. }
  12. }

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:

确定