2

I've created the class Course which contains the array students. This array contains student names which are in string form.

My new goal is to convert from an Array to an ArrayList. I'm not really sure how to go about this. I've read up on ArrayList and I believe it's resize-able which I think would work well in this case given the fact that the number of students might change constantly with my dropSutdent and addStudent method as opposed to setting an array size to 100, but only having 20 students.

I'd really appreciate and explanations/suggestions of how exactly to change to an ArrayList instead of an Array.

Note I apologize for any mistakes or if I left something unclear. This is my first question on StackOverflow and I know you guys are pretty strict on question asking, so I apologize in advance.*

    class Course {
    private String courseName;
    private String[] students = new String[100];
    private int numberOfStudents;

    public Course(String courseName) {
        this.courseName = courseName;
    }
    public void addStudent(String student) {
        int add = numberOfStudents - students.length; //Create integer to find how many slots we need
        if (numberOfStudents > students.length) {  //if # of students is bigger then size of array,
            String[] array = new String[students.length + add]; //then we add slots to the array.
            System.arraycopy(students, 0, array, 0, students.length); //copy array
            students = array;
        }
        students[numberOfStudents++] = student; //add student.
    }
    public String[] getStudents() {
        return students;
    }
    public int getNumberOfStudents() {
        return numberOfStudents;
    }
    public String getCourseName() {
        return courseName;
    }
    public void dropStudent(String student) {
        for (int i = 0; i < students.length; i++) {
            if (student == (students[i])) { //If student matches the student we want to remove.
                numberOfStudents--; //remove student
            while (i < numberOfStudents) {
                students[i] = students[i+1];
                        i++;
            }
          }
        }
     }
    public void clear() {
        students = new String[1];  //set size to 1
        numberOfStudents--; //remove all students
    }
}
2
  • Your understanding is rather fine. I think that you should try and comeback if you encounter some difficulties that you don't manage to solve.
    – davidxxx
    Commented Mar 28, 2018 at 21:51
  • It's straightforward if i understand you properly. simply change private String[] students = new String[100]; to thisList<String> students = new ArrayList<>(); then your addStudent method will internally call the list add method and the dropStudent will internally call the list remove method. also don't compare strings like this student == (students[i]) instead use the equals method.
    – Ousmane D.
    Commented Mar 28, 2018 at 21:52

2 Answers 2

3

List handles resizing internally, which means you can remove most of the boilerplate by using it instead of an array.

class Course {
    private String courseName;
    private List<String> students = new ArrayList<>();

    public Course(String courseName) {
        this.courseName = courseName;
    }
    public void addStudent(String student) {
        students.add(student);
    }
    public List<String> getStudents() {
        return students;
    }
    public int getNumberOfStudents() {
        return students.size();
    }
    public String getCourseName() {
        return courseName;
    }
    public void dropStudent(String student) {
        while (students.remove(student));
    }
    public void clear() {
        students.clear();
    }
}
0
0

ArrayLists are really usefull, but if you need more options, you can always create a class that works like list and implement all the methods as you wish.

Example for students:

public class studentList{

    int size;
    Student [] list;

    public studentList()
    {
        size = 0;
        list = new Student[size];
    }
    public studentList(int size)
    {
        this.size = size;
        list = new Student[size];
    }

    public void add(Student s)
    {

    }
    public void remove(Student s)
    {

    }
    public void sort()
    {

    }


}

You can implement as you wish.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.