【问题标题】:Link ArrayLists and save data on file链接 ArrayList 并将数据保存在文件中
【发布时间】:2015-10-17 01:36:44
【问题描述】:

我正在为一个学校项目开发 Java 应用程序,我们必须在其中输入用户信息:姓名、学生 ID 和他们的分数。如何将每个用户的所有数据存储在 ArrayList(或 Array 或任何类型)上,以便跟踪所有数据。

例子:

John Doe - 401712 - 20 分

杰克杨 - 664611 - 30 分

我希望能够调用 setPoints 之类的方法来更改学生选择的任何点值。

问题是:如何将 ArrayList 链接在一起。如果我有三个 ArrayList,Java 怎么知道什么名字、学生 id 和积分关联在一起?

所有的 ArrayList 都存储在一个名为 Data 的类中。

数据数据 = 新数据();

此外,Data 类中的所有 ArrayList 都应输出到一个文件,该文件将在下次打开应用程序时加载。

我会尽力回答任何问题。

【问题讨论】:

  • 很可能,您需要创建一个包含 3 个字段名称、学生 ID 及其分数的类,并定义一个具有该类类型的 arrayList。基本上,arrayList 的每个元素都包含一个包含您想要的对象的对象。而且您最终不会遇到很多复杂情况。

标签: java arraylist


【解决方案1】:

你需要创建一个名为Student的类,然后声明一个Student类型的数组/ArrayList。您的Student 类必须有一个构造函数来设置 Student 类实例的字段(创建的实例现在称为对象)。

所以首先在你的其他类所在的同一个包中创建一个 Student 类(你的 main 方法所在的类):

public class Student {

    private String firstName;
    private String lastName;
    private String studentId;
    private int points;

    public Student(String firstName, String lastName, String studentId, int points) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.studentId = studentId;
        this.points = points;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }        
}

然后在您的 main 方法或您喜欢的任何地方,创建一个 Hashmap 来保存您的 Student 对象。 map/hashmap 是一个集合,就像 ArrayList 一样,用于保存一组对象。在您的用例中,最好使用哈希图,因为使用哈希图查找/检索特定学生对象会更快、更容易。

import java.util.HashMap;
import java.util.Map;

public class Main {

    public static void main(String[] args) {
        // a map is a "key-value" store which helps you search items quickly 
        // (by only one lookup)
        // here you consider a unique value of each object as its 'key' in the map,
        // and you store the whole object as the value for that key.
        // that is why we defined Student as the second type in the following 
        // HashMap, it is the type of the "value" we are going to store
        // in each entry of this map. 
        Map<String, Student> students = new HashMap<String, Student>();

        Student john = new Student("John", "Doe", "401712", 20);
        Student jack = new Student("Jack", "Young", "664611", 30);

        students.put("401712", john);
        students.put("664611", jack);

        Student johnRetrieved = students.get("401712"); 
        // each hashmap has a get() method that retrieves the object with this 
        // specific "key". 
        // The following line retrieves the student object with the key "664611".
        Student jackRetrieved = students.get("664611");

        // set/overwrite the points "field" of this specific student "object" to 40
        johnRetrieved.setPoints(40);     
        int johnsPoints = johnRetrieved.getPoints(); 
        // the value of johnsPoints "local variable" should now be 40

    }
}

【讨论】:

    【解决方案2】:

    您需要定义一个包含3个数据字段的类,如下所示

    1. 姓名
    2. 学生证
    3. 他们的观点

    但不要忘记,该类必须具有类的其他必要元素,例如:

    1. 构造函数
    2. 必要时重载构造函数
    3. 访问器
    4. 突变体

    注意:为了访问你的arrayList中一个对象的每个部分,你可以使用访问器。为了操作你的arrayList中一个对象的每个部分,你可以使用mustators。

    有了这样的类后,你可以定义一个arrayList,其中包含你已经定义的类类型的元素

    喜欢:

    List<Your Type of class > students = new ArrayList<Your Type of class>;
    

    Java 7之后就可以了

    List<Your Type of class > students = new ArrayList<>;
    

    这是钻石推理。

    如果您要在 arrayList 中查找特定的 id 编号,您可以执行以下操作:

    public boolean findIdNumber(int idNumber){
        for(int i=0; i< students.size; i++)
              if(students.get(i).getID() == idNumber)
                   return true;
              else
                   return false;
    } 
    

    警告

    我所做的是建议您能够更顺畅地寻找您想要的东西。您需要进行必要的更改以符合您的要求 要求做

    【讨论】:

    • 我怎么能说 getPoints(int studentID) 并让它在 ArrayList 中搜索 ID 并返回与学生关联的点值。
    • 正如我所说,你将使用你的访问器来访问部件,如果它等于你正在寻找的,你做你想做的,如果不是,你继续搜索.如果不清楚,请大家知道
    • 它不应该返回一个布尔值。它应该返回一个带有点值的 int。
    • @JacksonWelch 我所做的只是一个建议。您需要进行必要的更改以符合您的要求。
    【解决方案3】:

    经典的面向对象方法是创建一个包含名称、ID 和点数的 Student 类,并将 Student 对象的列表存储在单个 ArrayList 中。

    class Student{
        private String id;
        private String name;
        private int points;
    
        public Student(String id, String, name, int points){
            this.id = id;
            this.name = name;
            this.points = points;
        }
    }
    ..
    ArrayList<Student> students = new ArrayList<Student>();
    
    students.add(new Student(1, 'John Doe', 1000));
    
    String id = students.get(0).id; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-02
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      相关资源
      最近更新 更多