亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 132 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號