gpt4 book ai didi

java - 如何停止模型类中的自动排序字段?

转载 作者:行者123 更新时间:2023-12-01 17:47:03 27 4
gpt4 key购买 nike

我创建了一个模型类(此处仅显示构造函数)

public PieceModel(int position, int piece_id) {
this.position = position;
this.piece_id = piece_id;
}

然后添加一些数据

list.add(new PieceModel(1, 100));
list.add(new PieceModel(2, 300));
list.add(new PieceModel(3, 600));

然后我将列表转换为字符串,并像这样打印

[{"piece_id":100,"position":1},{"piece_id":300,"position":2},{"piece_id":600,"position":3}]

但我需要这样

[{"position":1,"piece_id":100},{"position":2,"piece_id":200},{"position":3,"piece_id":300}]

请给一些建议..

最佳答案

有这样的事吗?覆盖 toString 并重新排列属性?

static class PieceModel {
private int position;
private int piece_id;

public PieceModel(int position, int piece_id) {
this.position = position;
this.piece_id = piece_id;
}

@Override
public String toString() {
return "{" +
"position=" + position +
", piece_id=" + piece_id +
'}';
}
}

public static void main(String[] args) {
List<PieceModel> list = new ArrayList<>();
list.add(new PieceModel(1, 100));
list.add(new PieceModel(2, 300));
list.add(new PieceModel(3, 600));
System.out.print(list.toString());
}

结果:

[{"position":1,"piece_id":100},{"position":2,"piece_id":200},{"position":3,"piece_id":300}]

关于java - 如何停止模型类中的自动排序字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53985941/

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