Link ArrayLists and save data to file

I am working on a Java application for a school project where we have to enter user information: name, student ID and their dots. How can I store all data for each user in an ArrayList (or Array or really any type) so that I can keep track of all data.

Example:

John Doe - 401712 - 20 points

Jack Young - 664611 - 30 points

I want to be able to call methods like setPoints to change the point values ​​for a selected student.

Here's the problem: how can I link the ArrayList together. If I have three ArrayLists, how does Java know which name, student ID and dots are related to each other?

All ArrayLists are stored in the Data class.

Data data = new data ();

In addition, all ArrayLists in the Data class should be output to a file that will be loaded the next time the application is opened.

I will try to answer any questions.

+3


source to share


3 answers


You need to define a class containing 3 data fields s as follows

  • Name
  • Student ID
  • their points

But do not forget that the class must have other required class members, for example:

  • Constructor
  • Overloaded constructors, if needed
  • Accessors
  • Mutators

Note . You can use accessors to access each part of an object in arrayList. You can use delimiters to manipulate each part of the object in the arrayList.

After such a class, you can define an arrayList containing elements with the class type you have already defined

Like:

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

      



After Java 7

you can do

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

      

which is a diamond conclusion.

If you are looking for a specific id number in your list of arrays, you can do something like:

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

      

Warning :

what i did are suggestions for you to be able to search for what you want to flatten. You need to make the necessary changes to accomplish what you asked to do

+2


source


You need to create a class named Student

and then declare an array / ArrayList of type Student. Your class Student

must have a constructor that sets the fields of the Student instance (the instantiated instance is now called an object).

So, first create the Student class in the same package as your other class (the class your method is in 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;
    }        
}

      



Then, in your method main

or wherever, create a Hashmap to hold the Student objects. A map / hashmap is a collection, similar to ArrayList, for storing a collection of objects. In your use case, it is better to use a hashmap because finding / retrieving a specific student object is much faster and easier when using a hashmap.

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


source


A classic OO approach would be to create a Student class including name, ID and dots, and store a list of Student objects in a single 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; 

      

0


source







All Articles