如何将列表变量传递到构造函数/方法中

我是 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”未定义。”


我需要如何消除过敏的帮助


扬帆大鱼
浏览 115回答 4
4回答

慕慕森

您需要 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"));

湖上湖

public class Patient {private String patientfirstName;private String patientLastName;private List<String> allergyList;&nbsp;public Patient(String patientfirstName, String patientLastName,&nbsp;&nbsp;List<String> allergyList) {&nbsp;this.patientfirstName = patientfirstName;&nbsp;this.patientLastName = patientLastName;&nbsp;this.allergyList = allergyList;&nbsp; }&nbsp;*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:&nbsp;// first create a list and add the value to it&nbsp;List<String> list = new ArrayList<>();&nbsp;list.add("allergy1");&nbsp;// now create a object and pass the list along with other variables&nbsp;Patient patientobj = new Patient("sean","john",list);

慕虎7371278

在我看来,你可以使用Varargs。感谢 varargs,您可以在参数中放入您想要的参数数量public class Patient {public String patientfirstName;public String patientLastName;public List<String> allergyList;public Patient(String fName,String lName,String...aList) {&nbsp; &nbsp; this.patientfirstName = fName;&nbsp; &nbsp; this.patientLastName = lName;&nbsp; &nbsp; this.allergyList = Arrays.asList(aList);}public static void main(String[] args) {&nbsp; &nbsp; Patient firstPatient = new Patient("Foo", "Bar", "First Allergy","Second Allergy");&nbsp; &nbsp; Patient secondPatient = new Patient("Foo", "Baz", "First Allergy","Second Allergy","Third Allergy","Fourth Allergy");&nbsp; &nbsp; Patient ThirdPatient = new Patient("Foo", "Foo", "First Allergy");}参数“aList”就像一个数组,因为varargs就像一个没有特定长度的数组,你在输入参数时选择的长度,如你所见allergyList 的类型是可以选择的。您也可以这样做:在“患者”属性中:&nbsp;public String[] allergyList;在构造函数中:public Patient(String fName,String lName,String...aList) {&nbsp; &nbsp; &nbsp; &nbsp; this.patientfirstName = fName;&nbsp; &nbsp; &nbsp; &nbsp; this.patientLastName = lName;&nbsp; &nbsp; &nbsp; &nbsp; this.allergyList = allergyList;&nbsp; &nbsp; }

泛舟湖上清波郎朗

您还有一种解决方案,只需添加该类的一个构造函数即可Patient。public Patient (String patientfirstName,String patientLastName,String allergeyList){this.patientfirstName&nbsp; = patientfirstName;this.patientLastName = patientLastName;\this.allergeyList = new ArrayList<>( Arrays.asList(allergeyList));}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java