How to pass a list variable into a constructor/method

感情迁移 提交于 2020-11-30 00:01:35

问题


I'm new to Java. I'm having issues passing a List variable that is part of a constructor definition when I created an object of the class

    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");
     gives an error -"constructor "Str,str,str" not deined. I need help 
     on how to pass the allergy

回答1:


Your need a List<String> instead of a single String, Arrays.asList(T...) is probably the easiest solution:

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

And if you have more allergies

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



回答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);



回答3:


You also has a solutionis just add one constructor of the class Patient.

public Patient (String patientfirstName,String patientLastName,String allergeyList){
this.patientfirstName  = patientfirstName;
this.patientLastName = patientLastName;\
this.allergeyList = new ArrayList<>( Arrays.asList(allergeyList));
}



回答4:


In my opinion, you could use Varargs. Thanks to varargs you can put into the parameters how many arguments do you want

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");
}

The parameter "aList" is like an array because varargs is like an array with no specific lenght,the length you choose when you enter the parameters, as you can see

The type of allergyList is by choice.. you can also do this:

In "Patient" attributes:

 public String[] allergyList;

In the costructor:

public Patient(String fName,String lName,String...aList) {
        this.patientfirstName = fName;
        this.patientLastName = lName;
        this.allergyList = allergyList;
    }


来源:https://stackoverflow.com/questions/57797497/how-to-pass-a-list-variable-into-a-constructor-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!