Method for adding objects to a fixed collection (Array) in Java

I created an inheritance hierarchy with one superclass called Employe and two subclasses called Lecturer and Assistant. In addition to this, I created a class called Subject, which has an array of employees.

What I want to do here is create a method to add Employe objects to an array.
I did the same which works for ArrayList, but it doesn't work for arrays.

If possible, how can I create a method to do the same with arrays?

public class Subject {

    private String subjectcode;
    private Employe[] employees;

    public Subject(String subjectcode) {
        this.subjectcode = subjectcode;
        Employe[] employees = new Employe[5];
    }

    public void setSubjectcode(String code) {
        this.subjectcode = code;
    }

    public String getSubjectcode() {
        return this.subjectcode;
    }

    public boolean addStaff(Employe employe) {
        if (employe instanceof Lecturer || employe instanceof Assistant) {
            this.employees.add(employe);
            return true;
        } else {
            return false;
        }
    }
}

      

+1


source to share


3 answers


You need to use ArrayList:

public class Subject
{   
    private String subjectcode;
    private final List<Employee> employees = new ArrayList<Employee>();

    public Subject(String subjectcode){
        this.subjectcode = subjectcode;
}

public boolean addStaff(Employe employe){
        return this.employees.add(employe);
 }

      



Or if you still want to use an array:

public boolean addStaff(Employe employe){
        List<Employee> tempList = Arrays.asList(this.employees);
    boolean added = tempList.add(employe);
    this.employees = tempList.toArray(this.employees);
    return added;
 }

      

+2


source


Arrays cannot grow or shrink dynamically on their own, since ArrayList

do, so the method add()

does not work, it will stop working after the array instance is full.

What you have with arrays, essentially, get(index)

and set(index, value)

, so when you know you will have the maximum number of N

employees, Subject

might look like this:

public class Subject {   

    private static final int N = 5;
    private String subjectcode;
    private Employe[] employees = new Employe[N];
    private int size = 0;

    public Subject(String subjectcode){
        this.subjectcode = subjectcode;
    }

    public void setSubjectcode(String code){
        this.subjectcode = code;
    }

    public String getSubjectcode(){
        return this.subjectcode;
    }

    public boolean addStaff(Employe employe){
        if (size == employees.length) {
          // cannot add when is full
          return false;
        }

        if(employe instanceof Lecturer || employe instanceof Assistant){
            this.employees[size++] = employe;
            return true;
        }
        return false;
    }
}

      

On the other hand, if you don't know how many employees a Subject may have even at the time of creation Subject

(if you know it, you can pass it N

as a constructor argument), you will need to inject a method to expand the internal array and call it whenever added a new desktop that may look like this:



private void ensureCapacity(int n) {
    int oldCapacity = employees.length;
    if (oldCapacity >= n) {
        // there nothing to do
        return;
    }

    // grow at least in half, to minimize copying data on each add
    int newCapacity = oldCapacity + (oldCapacity >> 1);
    if (newCapacity - n < 0)
        newCapacity = n;
    employees = Arrays.copyOf(employees, newCapacity);
}

public boolean addStaff(Employe employe) {
    ensureCapacity(size + 1);
    if (employe instanceof Lecturer || employe instanceof Assistant) {
       this.employees[size++] = employe;
       return true;
    }
    return false;
}

      

For a better example of growing arrays, see the standard implementation ArrayList

ensureCapacity(int minCapacity)

in the JDK.

But then again, this growing contraction is simply to repeat what has already been done ArrayList

for you.

+2


source


In the case of Java arrays, unlike ArrayList, you don't have an add method. So you cannot add like this. The array works like this:

        String[] employees = new String[5];
        employees[0] = "ad";

      

So, for an array, an index-based approach is needed, where you indicate that at index 0 we put this element, we put this element at index 1, etc. employees[0] = "as";

In your case, why do you need to use an array? I think ArrayList is the best fit according to the information you provided.

+1


source







All Articles