英文:
change String in an Arraylist java
问题
以下是您要翻译的内容:
我试图更改ArrayList中的一个字符串,但在能够访问正确位置以进行更改方面遇到了一些问题。
所以我尝试使用students.get(i).getFirstName() = newFirstName;
,但实际上这只是我尝试不同方法的一部分。
以下是我正在尝试更改firstName和lastName的代码。这是我的editStudentId
方法。
public void editStudentID(int findStudentId) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() != findStudentId) {
continue;
}
System.out.println("找到包含id为" + findStudentId + "的信息:");
System.out.println("您想在配置文件中进行哪些更改?");
System.out.println("1.名字");
System.out.println("2.姓氏");
int decision = scanner.nextInt();
switch (decision) {
case 1:
System.out.println("输入一个新的名字继续");
String newFirstName = scanner.next();
// 这是我目前正在处理的部分,但我在进行此操作时遇到了一些问题
students.get(i).setFirstName(newFirstName);
break;
case 2:
System.out.println("输入一个新的姓氏继续");
String newLastName = scanner.next();
// 同样,这里是更改姓氏的部分
students.get(i).setLastName(newLastName);
break;
}
return;
}
System.out.println("未找到ID");
}
以下是Student
类:
public class Student {
private final int id;
private final String firstName;
private final String lastName;
private final String dob;
public Student(int id, String firstName, String lastName, String dob) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getDob() {
return dob;
}
public void setFirstName(String newFirstName) {
// 设置新的名字
this.firstName = newFirstName;
}
public void setLastName(String newLastName) {
// 设置新的姓氏
this.lastName = newLastName;
}
public static Student createStudentID(int id, String firstName, String lastName, String dob) {
return new Student(id, firstName, lastName, dob);
}
}
英文:
I am trying to change a String in an Arraylist but having some trouble being able to access the correct position to change.
So I am tried using students.get(i).getFirstName() = newFirstName;
but I was just really just trying different things at this point.
Below is the code I am trying to make changes for firstName and lastName. This is my editStudentId
method
public void editStudentID(int findStudentId) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() != findStudentId) {
continue;
}
System.out.println("Found a profile containing information for " + findStudentId + ":");
System.out.println("What would you like to change in your profile?");
System.out.println("1.First Name");
System.out.println("2.Last Name");
int decision = scanner.nextInt();
switch (decision) {
case 1:
System.out.println("Enter a new first name to continue");
String newFirstName = scanner.next();
//so this is the part when I am currently working on but am having some trouble doing so
students.get(i).getFirstName() = newFirstName;
break;
case 2:
System.out.println("Enter a new last name to continue");
break;
}
return;
}
System.out.println(" Id not found ");
}
Here is my class for Students
public class Student {
private final int id;
private final String firstName;
private final String lastName;
private final String dob;
public Student(int id,String firstName, String lastName, String dob) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.dob = dob;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getDob() {
return dob;
}
public static Student createStudentID(int id,String firstName, String lastName, String dob)
{
return new Student(id,firstName, lastName, dob);
}
}
答案1
得分: 1
你应该添加一个设置(set)函数:
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
对其他变量也是一样。设置(Setters)和获取(Getters)函数是你的朋友。然后调用这些函数。使用 get*
来使用变量,不是改变变量。使用 set
来改变变量。
students.get(i).setFirstName(newFirstName);
英文:
You should add a set function:
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
Same for the other variables. Setters and Getters are your friends.
Then call these functions. Use get*
to use the variable, not change. Use set
to change the variable.
students.get(i).setFirstName(newFirstName);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论