首页 > 解决方案 > 在 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为我写的iddob不被用户更改

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);
    }

}

标签: javaarraylist

解决方案


首先,您需要使您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 ");

}

推荐阅读