2015-06-07 29 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

相关问题