更改ArrayList中的字符串,Java。

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

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 &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();
//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(&quot;Enter a new last name to continue&quot;);


                    break;
            }


           
            return;
        }
        System.out.println(&quot; Id not found &quot;);

    }

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

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

发表评论

匿名网友

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

确定