用另一个数组填充对象数组会返回内存地址。

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

Filling an array of objects with another array returns memory address

问题

private Trip[] trips;
private int[] distanceTraveled = new int[] { 8, 6, 15, 5 };

for (int i = 0; i < distanceTraveled.length; i++) {
  trips[i] = new Trip(0, i, distanceTraveled[i]);
}
for (Trip trip: trips) {
  System.out.println(trip.toString());
}

我试图将distanceTraveled数组中的值填充到Trip[]对象数组中,但是这段代码返回了以下内容。任何帮助将不胜感激。

@20ce73ec
@393679df
@56627197
英文:
private Trip[] trips;
private int[] distanceTraveled = new int[] { 8, 6, 15, 5 };

for (int i = 0; i &lt; distanceTraveled.length; i++) {
  trips[i] = new Trip(0, i, distanceTraveled[i]);
}
for (Trip trip: trips) {
  System.out.println(trip.toString());
}

I am trying to write the values in distanceTraveled array to fill the array of objects for Trip[], but this code returns. Any help will be appreciated

@20ce73ec
@393679df
@56627197

答案1

得分: 0

Override toString() method in Trip class.

> Refer: https://www.geeksforgeeks.org/overriding-tostring-method-in-java/

英文:

Override toString() method in Trip class.

> Refer: https://www.geeksforgeeks.org/overriding-tostring-method-in-java/

huangapple
  • 本文由 发表于 2020年10月13日 10:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64327833.html
匿名

发表评论

匿名网友

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

确定