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

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

如何將列表變量傳遞到構造函數/方法中

如何將列表變量傳遞到構造函數/方法中

揚帆大魚 2023-07-13 16:52:34
我是 Java 新手。當我創建類的對象時,我在傳遞作為構造函數定義一部分的列表變量時遇到問題    public class Patient {    private String patientfirstName;    private String patientLastName;    private List<String> allergyList;     public Patient(String patientfirstName, String patientLastName,      List<String> allergyList) {     this.patientfirstName = patientfirstName;     this.patientLastName = patientLastName;     this.allergyList = allergyList;      }     Patient patientobj = new Patient("sean","john","allegry1");給出錯誤:“構造函數“Str,str,str”未定義?!蔽倚枰绾蜗^敏的幫助
查看完整描述

4 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

您需要 aList<String>而不是單個String,Arrays.asList(T...)可能是最簡單的解決方案:

Patient?patientobj?=?new?Patient("sean",?"john",?Arrays.asList("allergy1"));

如果你有更多的過敏癥

Patient?patientobj?=?new?Patient("sean",?"john",?
????????Arrays.asList("allergy1",?"allergy2"));


查看完整回答
反對 回復 2023-07-13
?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

public class Patient {


private String patientfirstName;

private String patientLastName;

private List<String> allergyList;



 public Patient(String patientfirstName, String patientLastName, 

 List<String> allergyList) {

 this.patientfirstName = patientfirstName;

 this.patientLastName = patientLastName;

 this.allergyList = allergyList;

  }





 *Patient patientobj = new Patient("sean","john","allegry1");*// this is wrong you have to pass a list not the string. you should do something like this:


 // first create a list and add the value to it

 List<String> list = new ArrayList<>();

 list.add("allergy1");


 // now create a object and pass the list along with other variables

 Patient patientobj = new Patient("sean","john",list);


查看完整回答
反對 回復 2023-07-13
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

在我看來,你可以使用Varargs。感謝 varargs,您可以在參數中放入您想要的參數數量


public class Patient {


public String patientfirstName;

public String patientLastName;

public List<String> allergyList;




public Patient(String fName,String lName,String...aList) {

    this.patientfirstName = fName;

    this.patientLastName = lName;

    this.allergyList = Arrays.asList(aList);

}



public static void main(String[] args) {


    Patient firstPatient = new Patient("Foo", "Bar", "First Allergy","Second Allergy");


    Patient secondPatient = new Patient("Foo", "Baz", "First Allergy","Second Allergy","Third Allergy","Fourth Allergy");


    Patient ThirdPatient = new Patient("Foo", "Foo", "First Allergy");

}

參數“aList”就像一個數組,因為varargs就像一個沒有特定長度的數組,你在輸入參數時選擇的長度,如你所見


allergyList 的類型是可以選擇的。您也可以這樣做:


在“患者”屬性中:


 public String[] allergyList;

在構造函數中:


public Patient(String fName,String lName,String...aList) {

        this.patientfirstName = fName;

        this.patientLastName = lName;

        this.allergyList = allergyList;

    }


查看完整回答
反對 回復 2023-07-13
?
泛舟湖上清波郎朗

TA貢獻1818條經驗 獲得超3個贊

您還有一種解決方案,只需添加該類的一個構造函數即可Patient。


public Patient (String patientfirstName,String patientLastName,String allergeyList){

this.patientfirstName  = patientfirstName;

this.patientLastName = patientLastName;\

this.allergeyList = new ArrayList<>( Arrays.asList(allergeyList));

}


查看完整回答
反對 回復 2023-07-13
  • 4 回答
  • 0 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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