java - 在 Java 中编辑和更新 ArrayList
问题描述
我试图通过允许用户更改他们的名字和姓氏来更新 myArray 列表,同时保持 arraylist 中的其他信息相同。
代码如下
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 ");
}
这是我的Student
课程,我只final
为我写的id
,dob
不被用户更改
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);
}
}
解决方案
首先,您需要使您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("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 ");
}
推荐阅读
- javascript - 如何允许用户使用 php html css js 在 wyswg 中自定义电子商务商店主题
- c# - 如何在.net core中为多个区域定义路由
- php - 如何将 Codeigniter 方法的返回整数作为整个控制器的全局变量?
- apache-kafka-connect - 升级单个连接器是否需要重新启动工作器?
- git - git 的 --pretty 选项字符串格式的别名
- mysql - 具有意外结果的棘手 JOIN 问题
- laravel - 我应该在 Laravel 路由中使用哪种 http 请求方法?
- performance - 如何减小 Flexdashboards 的大小
- android - 无法获得 GridView 的扩展功能
- c# - 如何在 C# 中向现有 XML 添加新的 XmlElement?