英文:
Edit and Update ArrayList in Java
问题
我正在尝试通过允许用户更改其名字的方式来更新 myArray 列表,同时保持数组列表中的其他信息不变。
以下是代码部分:
public void editStudentID(int findStudentId) {
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() != findStudentId) {
continue;
}
System.out.println("找到包含 " + 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();//需要找到一种方法在我的数组列表中更新此项
break;
case 2:
System.out.println("请输入新的姓氏以继续");
String newLastName = scanner.next();//同样需要
break;
}
return;
}
System.out.println("未找到 Id ");
}
这是我的 Student
类,在其中仅对 id
和 dob
写了 final
,以防止用户更改:
public class Student {
private final int id;
private String firstName;
private 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);
}
}
英文:
I am trying to update myArray list by allowing the user to change their first and last name while keeping other information in the arraylist the same.
the code follows
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();//need to find a way to update this in my arraylist
break;
case 2:
System.out.println("Enter a new last name to continue");
String newLastName = scanner.next();//this as well
break;
}
return;
}
System.out.println(" Id not found ");
}
this is my Student
class where I only wrote final
for only my id
and dob
to not be changed by the user
public class Student {
private final int id;
private String firstName;
private 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
首先,您需要使您的Student
类可变,为此需要提供一些“设置器”方法,这将允许您更改名字属性,例如
public class Student {
//...
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = 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("找到包含有关 " + 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();//需要找到一种方法在我的ArrayList中更新这个
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 ");
}
英文:
First, you need to make you Student
class mutable, but supplying a couple of "setters" which will allow you to change the first and last name properties, for example
public class Student {
//...
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Then in you editStudentID
method, you simply update the required record, for example
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();//need to find a way to update this in my arraylist
students.get(i).setFirstName(newFirstName);
break;
case 2:
System.out.println("Enter a new last name to continue");
String newLastName = scanner.next();//this as well
students.get(i).setLastName(newFirstName);
break;
}
return;
}
System.out.println(" Id not found ");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论