简体   繁体   English

如何在内存中存储arrayList的不同对象并稍后检索它们?

[英]How to store different objects of arrayList in memory and later retrieve them?

I am new to Java, and I am stuck at a place here.我是 Java 新手,我被困在这里的一个地方。 I have a student class that has some variables like name, contact no, etc. I have created a method addStuDetails to store the details of student in main class by taking values of these variables from user using scanner.我有一个学生类,它有一些变量,如姓名、联系人号码等。我创建了一个方法 addStuDetails,通过使用扫描仪从用户那里获取这些变量的值来将学生的详细信息存储在主类中。

public class Student extends Person {

private int rollNumber, marks;

public Student(String name, String add, int contactNo, int roll, int mark) {
    super(name, add, contactNo);
    this.rollNumber = roll;
    this.marks = mark;
}

public String toString() {
    return "Name: " + Name + " ,Address: " + Address + " ,Contact no:" + ContactNumber + " ,Roll no.: " + rollNumber
            + " ,Marks :" + marks;

}

public void addStuDetails(Student s) {
    List<Student> stu = new ArrayList<Student>();
    Student sObj = new Student(Name, Address, ContactNumber, rollNumber, marks);
    stu.add(sObj);
    System.out.println(stu);
}

} }

Following in the main class where the user enters the values:在用户输入值的主类中:

public class Main {

 public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the details of student: ");

    System.out.print("Enter name: ");
    String name = input.nextLine();
    System.out.println("Enter address: ");
    String address = input.nextLine();
    System.out.println("Enter contact no: ");
    int no = input.nextInt();
    System.out.println("Enter rollno: ");
    int roll = input.nextInt();
    System.out.println("Enter marks");
    int marks = input.nextInt();
    Student s = new Student(name,address,no,roll, marks);
    s.addStuDetails(s);     
}

The details are added in the above created arrayList and gets printed.详细信息添加到上面创建的 arrayList 中并打印出来。 Now, I want to store all the students that user created in the mail class in the memory and then retrieve the same using roll no.现在,我想将用户在邮件类中创建的所有学生存储在内存中,然后使用卷号检索它们。 Can anyone please help on how to do the same?任何人都可以请帮助如何做同样的事情? Please note that I don't want to store these students in ant database or file, only in the memory.请注意,我不想将这些学生存储在 ant 数据库或文件中,只想存储在内存中。

It doesn't make sense for Student to contain a List of students, it's beyond it's scope of responsibility. Student包含Student List是没有意义的,这超出了它的职责范围。

Instead, List should be independent of it, for example...相反, List应该独立于它,例如......

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    private List<Student> students = new ArrayList<Student>();

    public Main() {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter the details of student: ");

        System.out.print("Enter name: ");
        String name = input.nextLine();
        System.out.println("Enter address: ");
        String address = input.nextLine();
        System.out.println("Enter contact no: ");
        int no = input.nextInt();
        System.out.println("Enter rollno: ");
        int roll = input.nextInt();
        System.out.println("Enter marks");
        int marks = input.nextInt();
        Student s = new Student(name, address, no, roll, marks);
        students.add(s);
    }
}

nb: I have a personal dislike for static , as it tends to lead new developers to bad habits 😝注意:我个人不喜欢static ,因为它往往会导致新开发人员养成坏习惯😝

Equally, if you wanted to encapsulate the "student" functionality, you could create a student "manager" class, which maintained the list internally and exposed functionality that could be performed upon it, but that might be beyond the scope of this question同样,如果您想封装“学生”功能,您可以创建一个学生“经理”类,该类在内部维护列表并公开可以对其执行的功能,但这可能超出了本问题的范围

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

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