我正在尝试为 数组中的字符串变量 分配值:
String details = "passFirst0,passLast0,Molly,Quinn";
Passenger passenger = new Passenger(details);
public class Passenger {
private String firstNameField;
private String lastNameField;
private String firstName;
private String lastName;
public Passenger(String details) {
String[] temp = details.split(",");
String[] fields = {firstNameField, lastNameField, firstName, lastName};
for (int ctr = 0; ctr < fields.length; ctr++) {
fields[ctr] = temp[ctr];
}
// Print instance variables - all null
System.out.println(this.firstNameField);
System.out.println(this.lastNameField);
System.out.println(this.firstName);
System.out.println(this.lastName);
// Print array - has values
System.out.println(Arrays.toString(fields));
}
// Methods
}
但是,实例变量本身保持为空,而fields[]
在迭代数组时具有值。
为什么会如此以及如何实现这一目标?
答案 0 :(得分:3)
我建议不要这样做,但你可以使用Java 8方法引用:
public class Passenger {
private String firstNameField;
private String lastNameField;
private String firstName;
private String lastName;
public Passenger(String details) {
List<Consumer<String>> setters = Arrays.asList(this::setFirstNameField,
this::setLastNameField,
this::setFirstName,
this::setLastName);
String[] temp = details.split(",");
for (int i = 0; i < setters.size(); i++) {
setters.get(i).accept(temp[i]);
}
}
public String getFirstNameField() {
return this.firstNameField;
}
public void setFirstNameField(String firstNameField) {
this.firstNameField = firstNameField;
}
public String getLastNameField() {
return this.lastNameField;
}
public void setLastNameField(String lastNameField) {
this.lastNameField = lastNameField;
}
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Passenger [firstNameField=" + this.firstNameField +
", lastNameField=" + this.lastNameField +
", firstName=" + this.firstName +
", lastName=" + this.lastName + "]";
}
}
测试
String details = "passFirst0,passLast0,Molly,Quinn";
Passenger passenger = new Passenger(details);
System.out.println(passenger);
输出
Passenger [firstNameField=passFirst0, lastNameField=passLast0, firstName=Molly, lastName=Quinn]
答案 1 :(得分:3)
字符串对象是不可变的 - 您无法更改它们的值 - 在执行赋值时,您正在更改变量(或给定数组索引处的变量)所引用的对象。
当你这样做时:
String[] fields = {firstNameField, lastNameField, firstName, lastName};
您将fields
数组值index
0的引用设置为firstNameField
引用的同一对象(在本例中为null
),index
1引用与lastNameField
等相同的对象
然后,如果你这样做:
fields[ctr] = temp[ctr];
你不更改了对象fields[ctr]
之前引用的值(您的一个实例变量)到temp [ctr]的值,而是设置{{1} }引用现在指的同一个对象fields[ctr]
。
如果你想把你的变量称为一个数组,为什么不能从头开始将它们声明为一个数组呢?
答案 2 :(得分:1)
它们为空,因为它们尚未设置,您可以像这样设置它们;
public class Passenger {
private String firstNameField = "a";
private String lastNameField = "b";
private String firstName = "c";
private String lastName "d";
或者constructor
这样的参数;
public Passenger(String details, String f, String l, String fn, String ln) {
firstNameField = a;
lastNameField = l;
firstName = fn;
lastName = ln;
.....
}
如评论中所述,您确实会覆盖for循环中的数组值,但是您尝试打印出类成员;
System.out.println(this.firstNameField);
System.out.println(this.lastNameField);
System.out.println(this.firstName);
System.out.println(this.lastName);
未经初始化。
为什么不像下面那样设置数组;
String[] temp = details.split(",");
String[] fields = {temp[0], temp[1], temp[2], temp[3]};
为什么要为其他变量而烦恼,只需为每个所需变量创建getters
,例如;
public String getFirstName() {
return fields[2];
}
答案 3 :(得分:0)
在“字段”中,您具有在for循环中设置的“详细信息”拆分值以及实例变量中没有任何内容(它们未使用数据初始化)
答案 4 :(得分:0)
至于&#34;怎么&#34;,我最终选择了Andreas&#39;第一条评论:
public Passenger(String details) {
String[] temp = details.split(",");
this.firstNameField = temp[0];
this.lastNameField = temp[1];
this.firstName = temp[2];
this.lastName = temp[3];
}
认识到最简单的方法非常糟糕。