我是Java新手。创建类对象时,传递作为构造函数定义一部分的List变量时遇到问题
.u.ups
答案 0 :(得分:3)
您需要一个List<String>
而不是一个String
,Arrays.asList(T...)
可能是最简单的解决方案:
Patient patientobj = new Patient("sean", "john", Arrays.asList("allergy1"));
如果您还有更多过敏反应
Patient patientobj = new Patient("sean", "john",
Arrays.asList("allergy1", "allergy2"));
答案 1 :(得分:1)
LinkedList<Integer> linkedList = new LinkedList<>();
long v3 = 10000_0000_00L;
long longmodulo= v3 % 10;
linkedList.add((int) longmodulo);
v3 = v3/10;
while (v3 != 0){
long longmodulo2= v3 % 10;
linkedList.add((int) longmodulo2);
v3 = v3/10;
System.out.println("value of v3 "+v3);
}
linkedList.forEach(System.out::println);
答案 2 :(得分:0)
您还有一个解决方案,就是只添加一个类Patient
的构造函数。
public Patient (String patientfirstName,String patientLastName,String allergeyList){
this.patientfirstName = patientfirstName;
this.patientLastName = patientLastName;\
this.allergeyList = new ArrayList<>( Arrays.asList(allergeyList));
}
答案 3 :(得分:0)
我认为您可以使用 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就像一个没有特定长度的数组,如您所见,输入参数时您选择的长度
过敏列表的类型是可选的。您也可以执行以下操作:
在“患者”属性中:
public String[] allergyList;
在构造函数中:
public Patient(String fName,String lName,String...aList) {
this.patientfirstName = fName;
this.patientLastName = lName;
this.allergyList = allergyList;
}