简体   繁体   English

如何将信息添加到arraylist中的现有元素?

[英]How can I add an information to an existing element in an arraylist?

have a question about this code. 对这段代码有疑问。 How can I add information to an existing element? 如何将信息添加到现有元素? For example in the beginning the machine asks the user to give a name. 例如,在一开始,机器要求用户输入名称。 If I give the name "Harry" the machine will just say "the student name is correct" but I want also to see the age of that student and in general some information. 如果我给名字“ Harry”,机器将只说“学生姓名正确”,但我也想查看该学生的年龄以及一般情况下的一些信息。 So my question is how can I add informations for each student? 所以我的问题是如何为每个学生添加信息? Here is my code so far. 到目前为止,这是我的代码。 Thanks in advance! 提前致谢!

package test;

import java.util.*;

public class readStudents {
    public static void main(String []args) {




        ArrayList<String> arrlstStr = new ArrayList<String>(); //Declaring ArrayList

        arrlstStr.add("Malte");
        arrlstStr.add("Timo");
        arrlstStr.add("Harry");
        arrlstStr.add("Julian");
        arrlstStr.add("Musa");
        arrlstStr.add("Mirnes");
        arrlstStr.add("Daniel");
        arrlstStr.add("David");
        arrlstStr.add("Nico");
        arrlstStr.add("Ranya");
        arrlstStr.add("Xuan");
        arrlstStr.add("Sören");
        arrlstStr.add("Mark");
        arrlstStr.add("Salomon");
        arrlstStr.add("Leon");
        arrlstStr.add("Niklas");
        arrlstStr.add("Tobias");

        System.out.println("Enter the name of the student: ");
        Scanner scanner = new Scanner(System.in);
        String student = scanner.nextLine();

        if (arrlstStr.contains(student)) {
            System.out.println("This student name is correct");
        }
        else { 
            System.out.println("You gave a wrong name");
        }
    }
}
public class Student {
 private String name;
 private int age;

// other fields with getter and setter
}

public class StudentFields {

public static void main(String []args){
 ArrayList<Student> arrlstStr = new ArrayList<Student>(); //Declaring ArrayList
Student s1 = new Student();
s1.setName("R1");
s1.setAge(20);

    arrlstStr.add(s1);
    arrlstStr.add(s2);
}
}

A very simple thing to do :) 一个很简单的事情:)

You will agree with me that, array list is a collection of list of elements. 您将与我同意,数组列表是元素列表的集合。 This elements in your case were pre-defined. 在您的情况下,此元素是预定义的。 What I would suggest is that you have to also define the information you want any element to have. 我的建议是,您还必须定义任何元素想要的信息。 For example; 例如; student name 'Harry' should have a piece of corresponding information like age pointing to it; 学生姓名“哈里”应该有一条相应的信息,例如年龄指向它; student_age; 学生年龄; eg Harry_age set to '34'. 例如Harry_age设置为“ 34”。 something like this: 像这样的东西:

arrlstStr.add("Harry") ; arrlstStr.add(“ Harry”) ;

arrlstStr.add("Harry_age") arrlstStr.add(“ Harry_age”)

assigning value to the element 给元素赋值

Harry_age = 34; 哈里奇年龄= 34;

The above illustration is to guide you. 上图是指导您。 Once you have defined that, make you use of your if-statement. 定义完后,请使用if语句。 The pseudo-code is; 伪代码是; IF student name is "Harry" THEN DISPLAY 34 (Harry's age) 如果学生的名字是“哈利” 然后 显示 34(哈利的年龄)

This should work. 这应该工作。 Is just simple logic. 只是简单的逻辑。

As per your requirement I would recommend to use map instead of Array list and keep the student name as key and student object as value so on bases of name you can get student information.such as below 根据您的要求,我建议使用map而不是Array list并将学生姓名作为键并将学生对象保留为值,以便基于姓名可以获取学生信息。

public class Student {
 private String name;
 private int age;

 public Student(String name,int age)
 {
     this.name=name;
     this.age=age;
 }

 public String getName() {
    return name;
 }

 public int getAge() {
    return age;
 }

} }

public class StudentData {

public static void main(String []args){
Map<String,Student> studentCollection=new HashMap<String,Student>();
Student student = new Student("Sachin",40);
studentCollection.put(student.getName(),student);
.
.


}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM