gpt4 book ai didi

java - 显示数组的值。得到奇怪的值

转载 作者:行者123 更新时间:2023-12-02 01:17:05 25 4
gpt4 key购买 nike

我想制作 k 个 TockaXY 类型的随机点。TockaXY 定义为:

 public static class TockaXY {

private float x;
private float y;

public TockaXY(float x, float y) {
this.x = x;
this.y = y;
}

函数是这样的:

 public static TockaXY[] randomCentri(int k, int lowerBound, int upperBound) {
TockaXY[] arrayCentri = new TockaXY[k];
for (int i = 0; i < k; i++) {
float x = (float)(Math.random() * (upperBound - lowerBound) + lowerBound)/100;
float y = (float)(Math.random() * (upperBound - lowerBound) + lowerBound)/100;
TockaXY point = new TockaXY(x, y);
arrayCentri[i]=point;
}
System.out.println(" Random centri: " + arrayCentri);
return arrayCentri;

}

但我得到:

Random centri: [LTest$TockaXY;@6ce253f1

我做错了什么?

最佳答案

您需要在 TockaXY 类中添加 toString 方法

@Override
public String toString() {
return "TockaXY{" + "x=" + x + ", y=" + y + '}';
}

并在打印时使用 Array.toString

System.out.println(" Random centri: " + Arrays.toString(arrayCentri));

或者使用循环打印每个数组元素

for(TockaXY point: arrayCentri){
System.out.println(point);
}

关于java - 显示数组的值。得到奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58390181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com