Home > Back-end >  How to add new element to the end of array in Java
How to add new element to the end of array in Java

Time:01-19

StudentModel[] student = new StudentModel[] {
     new StudentModel(1, "Kanha", "Vong", "Female", "09/09/2000", "Siem Reap", "016663332"),
     new StudentModel(2, "Echrysa", "Chhy", "Male", "01/20/2000", "Pursat", "097222444"),
     new StudentModel(3, "Sopheak", "Chok", "female", "29/06/2005", "Battambang", "096565544"),
     new StudentModel(4, "Sakda", "Heang", "Male", "04/12/2001", "Banteay Meanchey", "097889900"),
     new StudentModel(5, "TongHan", "Khy", "Male", "03/06/2002", "Pursat", "0976543201"),
     new StudentModel(6, "Seyma", "Hor", "Female", "27/08/2004", "Battambang", "015765456")
};

public void insertStudent(StudentModel[] student){
    int n = student.length;
    student = new StudentModel[n 1];

    System.out.print("Enter Student ID:");
    student[n].setId(sc.nextInt());

    System.out.print("Enter First Name:");
    student[n].setFirstName(sc.nextLine());

    .......................................


}

I've got error message: Cannot invoke "StudentModel.setId(int)" because "student[n]" is null. Anyone can help to fix this? Thank you!

CodePudding user response:

Are you depended on Arrays?

If not you could use Map with List or two Lists. My guess:

privat Map<int,List<String>> students = new HashMap<int, List<String>>();

public void addStudent(int id,String name, String lastName, String birthdate,...){
   List<String> student = new LinkedList<String>(); //LinkedList for fixed order
   student.add(name);
   student.add(lastName);
   student.add(birthdate);

   students.put(id, student);
}

Should work like this and with LinkedList the order is fixed and you can get the data by knowing the index if needed

Otherwise you could try a List in a List.

Advantage here ist that you can easy access the data of the students by index and good methods!

CodePudding user response:

There are 2 step.

  1. Every time '[]' is added as a static array, a new address value is assigned and resources are wasted.

  2. Therefore, you should use List, a collection that allows you to add values dynamically.


    StudentModel[] student = new StudentModel[] {
            new StudentModel(1, "Kanha", "Vong", "Female", "09/09/2000", "Siem Reap", "016663332"),
            new StudentModel(2, "Echrysa", "Chhy", "Male", "01/20/2000", "Pursat", "097222444"),
            new StudentModel(3, "Sopheak", "Chok", "female", "29/06/2005", "Battambang", "096565544"),
            new StudentModel(4, "Sakda", "Heang", "Male", "04/12/2001", "Banteay Meanchey", "097889900"),
            new StudentModel(5, "TongHan", "Khy", "Male", "03/06/2002", "Pursat", "0976543201"),
            new StudentModel(6, "Seyma", "Hor", "Female", "27/08/2004", "Battambang", "015765456")
    };

    public void insertStudent(LinkedList student){
        // Creates one object with the default constructor.
        student.add(new StudentModel());
        // Find the location of the last element.
        int size = student.size() - 1;
        // Sets the value input from the user.
        System.out.print("Enter Student ID:");
        student[size].setId(sc.nextInt());

        System.out.print("Enter First Name:");
        student[size].setFirstName(sc.nextLine());
    }

I hope it helps.

  • Related