2015-06-07 30 views
1

我製作了一個繼承層次結構,其中一個名爲Employe的超類以及兩個名爲Lecturer和Assistant的子類。除此之外,我還創建了一個名爲Subject的類,其中包含一系列員工。將對象添加到Java中的固定集合(數組)中的方法

我想在這裏做的是創建一個方法,將Employe對象添加到數組中。
我做了與ArrayList一樣的工作,但它似乎不適用於數組。

如果可能,我怎樣才能創建一個方法來做與數組相同的事情?

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; 
     } 
    } 
} 
+0

你知道這裏發生了什麼:'僱員[]僱員=新僱員[5];'? – Tom

+0

有沒有理由用數組而不是'ArrayList'來做這件事?你確定你永遠只需要5名員工?你爲什麼要檢查員工是「講師」還是「助理」? – RealSkeptic

+0

我知道使用ArrayList會更容易,但我想知道是否可以用Array來做到這一點。如果沒有,那麼我只需要使用數組列表。 – Marius

回答

2

您需要使用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); 
} 

或者,如果你仍然想使用數組:

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; 
} 
+0

我想他說他做了一個和ArrayList一起工作的人,他想用數組來實現它,出於某種原因? – Gosu

+0

對不起,我誤讀了。無論如何,如果原因是在其他地方使用數組,你可以這樣做..employees.toArray() – aurya

+0

這就是正確的Gosu。 – Marius

1

在Java數組的情況下,與ArrayList的你不必添加方法。所以,你不能添加喜歡它。排列如下操作:

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

因此,陣列需要基於索引的方法,在這裏您可以指定索引0把這個元素,在索引1把這個元素,等等.... employees[0] = "as";

在你的情況下,爲什麼你需要使用數組?根據您提供的信息,我認爲ArrayList最合適。

2

數組不能自動增長或縮小,因爲ArrayList這樣做,這就是爲什麼沒有add()方法 - 它會在數組實例已滿後停止工作。

你有什麼用數組,實質上是一個get(index)set(index, value),所以,當你知道你將在最大N員工,Subject可能看起來像這樣:

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

在另一方面,如果您不知道在創建Subject時(如果您知道它,您可能通過N作爲構造函數參數),Subject可能有多少員工,您必須實現增長內部數組和調用的方法它每當新的僱員被添加,它可能看起來像這樣:

private void ensureCapacity(int n) { 
    int oldCapacity = employees.length; 
    if (oldCapacity >= n) { 
     // there's 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; 
} 

有關生長陣列的更好示例,請參閱JDK中的default implementationArrayListensureCapacity(int minCapacity)

但是,這種不斷增長的縮小的東西只是重新實現了ArrayList已經爲你做了什麼。

+0

你測試了你的代碼嗎?我聞到了強烈的'NullPointerException'氣味。 – Tom

+0

@Tom,是的,對不起,我在從IDE複製代碼並在此格式化時弄糟了。現在它應該被修復。 – sainaen

相關問題