为了账号安全,请及时绑定邮箱和手机立即绑定

Java - 我们如何使集合中的某个对象在索引 0 处返回;

Java - 我们如何使集合中的某个对象在索引 0 处返回;

弑天下 2022-11-30 10:12:59
假设我们有一个集合public Set<CarPartDto> carpartCarPartDto 在哪里public class CarPartDto {public String type;public String colour;public Long torque;public Long maxSpeed;public String manufacturer;我们如何制作一个列表,将第一个索引或索引 0 返回到所需的对象让我们说制造商预期结果是这样的            "manufacturer": nissan,            "type": "sedan",            "colour": "black",            "torque": 139,            "maxSpeed": 200,实际结果是这样的            "type": "sedan",            "colour": "black",            "torque": 139,            "maxSpeed": 200,            "manufacturer": nissan我是这个领域的新手。请帮忙!
查看完整描述

3 回答

?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

听起来您想将汽车的信息打印为字符串。在这种情况下,您需要覆盖 CarPartsDto 类中的 toString() 方法。


@Override

public String toString() {

    return "Manufacturer: " + manufacturer + "\n" +

            "Type: " + type + ",\n" +

            "Colour: " + colour + ",\n" +

            "Torque: " + torque + ",\n" +

            "MaxSpeed: " + maxSpeed;

}

要调用它,您只需要在不使用任何方法或使用 toString 方法的情况下调用您的对象。


for (CarPartDto car : cars) {

    System.out.println(car);

}

此外,如果您需要任何其他形式的信息,您也可以编写自己的方法并以您需要的任何格式返回它(在本例中为字符串):


public String returnCarInfo(){

    return "Type: " + type + ",\n" +

            "Colour: " + colour + ",\n" +

            "Torque: " + torque + ",\n" +

            "MaxSpeed: " + maxSpeed + ",\n" +

            "Manufacturer: " + manufacturer;

}

并使用该方法调用它。


System.out.println(car.returnCarInfo());

希望这可以帮助!


查看完整回答
反对 回复 2022-11-30
?
浮云间

TA贡献1829条经验 获得超4个赞

我不能发表评论,所以:


看起来您想订购类型只是简单地将您的课程更改为


public class CarPartDto {


 public String manufacturer;

 public String type;

 public String colour;

 public Long torque;

 public Long maxSpeed;

}

或者你可以创建一个方法(在你的类中)而不是返回你想要的格式对象:


        public String getCarInfo(){


        return "manufacturer: " + manufacturer + "\ntype: " + type + "\ncolour: "+colour + "\ntorque: " + torque + "\nmaxSpeed: " + maxSpeed;

    }


查看完整回答
反对 回复 2022-11-30
?
慕斯王

TA贡献1864条经验 获得超2个赞

仅回答标题,Java - 我们如何使集合中的某个对象在索引 0 处返回;

Sets 通常没有将用户友好的顺序作为设计目标,尽管某些实现确实有:TreeSet按自然顺序LinkedHashSet返回其元素,按插入顺序返回其元素。

你可以用一个简单的代码试试


Random r=new Random();

Set<Integer> treeset=new TreeSet<Integer>();

Set<Integer> linked=new LinkedHashSet<Integer>();

Set<Integer> simple=new HashSet<Integer>();

for(int i=0;i<10;i++){

    int n=r.nextInt(100);

    System.out.print(n+", ");

    treeset.add(n);

    linked.add(n);

    simple.add(n);

}

System.out.println();

for(Object i:treeset.toArray())

  System.out.print(i+", ");

System.out.println();

for(Object i:linked.toArray())

  System.out.print(i+", ");

for(Object i:simple.toArray())

  System.out.print(i+", ");

(https://ideone.com/Wz3o61 - 第一行是一堆随机数,第二行是TreeSet,有序,第三行是LinkedHashSet,保留输入顺序,最后一行是HashSet,具有任意顺序)。


因此,如果您的问题与Set-s 有关(在撰写本文时似乎并非如此),您可以通过首先使用LinkedHashSet和添加该元素来强制执行“第一个”元素,或者选择一种更深奥的方法/创建具有合适顺序的元素类 - 也许使用枚举。但问题更可能与打印对象有关,toString()即代码中某处的 like 方法。


查看完整回答
反对 回复 2022-11-30
  • 3 回答
  • 0 关注
  • 73 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信