PizzaParrot
PizzaParrot

Reputation: 121

Converting from an array to an array list in Java

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
    }
}

Upvotes: 2

Views: 216

Answers (2)

C.Aglar
C.Aglar

Reputation: 1319

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.

Upvotes: 0

shmosel
shmosel

Reputation: 50716

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();
    }
}

Upvotes: 3

Related Questions