编辑并更新Java中的ArrayList

huangapple go评论76阅读模式
英文:

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 类,在其中仅对 iddob 写了 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 &lt; students.size(); i++) {
            if (students.get(i).getId() != findStudentId) {
                continue;
            }
            System.out.println(&quot;Found a profile containing information for &quot; + findStudentId + &quot;:&quot;);
            System.out.println(&quot;What would you like to change in your profile?&quot;);
            System.out.println(&quot;1.First Name&quot;);
            System.out.println(&quot;2.Last Name&quot;);
            int decision = scanner.nextInt();
            switch (decision) {
                case 1:
                    System.out.println(&quot;Enter a new first name to continue&quot;);
                    String newFirstName = scanner.next();//need to find a way to update this in my arraylist
                    break;

                case 2:
                    System.out.println(&quot;Enter a new last name to continue&quot;);
                    String newLastName = scanner.next();//this as well
                    break;
            }
            return;
        }
        System.out.println(&quot; Id not found &quot;);

    }

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 &lt; students.size(); i++) {
        if (students.get(i).getId() != findStudentId) {
            continue;
        }
        System.out.println(&quot;Found a profile containing information for &quot; + findStudentId + &quot;:&quot;);
        System.out.println(&quot;What would you like to change in your profile?&quot;);
        System.out.println(&quot;1.First Name&quot;);
        System.out.println(&quot;2.Last Name&quot;);
        int decision = scanner.nextInt();
        switch (decision) {
            case 1:
                System.out.println(&quot;Enter a new first name to continue&quot;);
                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(&quot;Enter a new last name to continue&quot;);
                String newLastName = scanner.next();//this as well
                students.get(i).setLastName(newFirstName);
                break;
        }
        return;
    }
    System.out.println(&quot; Id not found &quot;);

}

huangapple
  • 本文由 发表于 2020年9月1日 11:21:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63680884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定