将此模板类转换为泛型类

时间:2012-10-31 15:49:42

标签: java generics template-classes

所以我是一名大学生,只是寻求一些帮助和理解,我有一位教授不允许我们使用java预编写的类,如ArrayList,所以我试图弄清楚如何修改我的当前封装的数组类使用泛型,这样我就不必在程序的应用程序类中进行这么多的转换

public class ArrayClass {
    private Object[]  objArray;
    private int index = 0;
    public static final int MAX_SIZE = 100;

    public ArrayClass(){
        objArray = new Object[100];
    }

    public ArrayClass(int numSlots){
        objArray = new Object[numSlots];
    }
    public ArrayClass(Object[] anArray, int newIndex){
        objArray = new Object[newIndex];
        for(int i=0; i<newIndex; i++){
            objArray[i] = anArray[i];
        }
        index = newIndex;
    }


    //return object array, accessor
    public Object[] getstrArr(){
        return objArray;
    }
    //return # of actual data in array, accessor
    public int getIndex(){
        return index;
    }
    //return an object at given pos, accesor
    public Object getObject(int pos){
        return objArray[pos];
    }


    //assign a new object array, mutator
    public void setObjArr(Object[] aStrArr){
        for(int i=0; i<index; i++){
            objArray[i] = aStrArr[i];
        }
    }
    //assign a new index, mutator
    public void setIndex(int anIndex){
        index = anIndex;
    }
    //insert a new string into the array if there is room, increment index
    public void add(Object someObj){
        if(index < objArray.length){
            objArray[index] = someObj;
            index++;
        }
    }

    //return the string with contents of array
    public String toString(){
        String output = " ";
        for(int i=0; i<index; i++){
            output = output + objArray[i].toString();
        }
        return output;
    }

    //return true if calling object is equivalent to argument
    public boolean equals(Object someObj){
        for(int i=0; i< index; i++){
            if(objArray[i].equals(someObj))
                return true;
        }
        return false;
    }
}

3 个答案:

答案 0 :(得分:3)

public class ArrayClass<E> {

// return object array, accessor
public <T> T[] getstrArr(T[] t) {<--This is done because arrays are covarant in nature    
       return (T[]) Arrays.copyOf(objArray, index, t.getClass());
    }

 // return an object at given pos, accesor
public E getObject(int pos) {<-- For single elements just return E with casting
    return (E) objArray[pos];
}

实施说明

  1. {/ 1}}
  2. 应考虑长度检查
  3. 您可以阅读有关generics
  4. 的精彩文章

答案 1 :(得分:1)

好吧,我不会用setIndex等方法公开你的索引。也不会添加null元素检查。

无论如何,我会做出类似的事情:

public class ArrayClass<E> {
    ...
    private E[] elements;
    private int index;
    ...
    public ArrayClass() {
        this.elements = (E[]) new Object[MAX_SIZE];
        this.index = 0;
    }
    // this constructor substitutes the method setObjArr
    public ArrayClass(E[] elements) {
        this.elements = elements;
        this.index = elements.length;
    }
    ...
    public void add(E element) {
        if (needsToGrow()) {
            duplicateArraySize();
        }
        this.elements[index++] = element;
    }
    private boolean needsToGrow() {
        return index + 1 == elements.length;
    }
    private void duplicateArraySize() {
        E[] extendedArray = (E[]) new Object[this.elements.length * 2];
        System.arraycopy(elements, 0, extendedArray, 0, elements.length);
        this.elements = extendedArray;
    }
    public E get(int index) {
        return this.elements[index];
    }
    public int size() {
        return this.index;
    }
    ...
}

答案 2 :(得分:0)

我会这样做:

public class ArrayClass<T> {
  public T[] objArray;
  private int index = 0;
  public static final int MAX_SIZE = 100;

  @SuppressWarnings("unchecked")
  public ArrayClass(Class<T> c) {
    objArray = (T[]) Array.newInstance(c,MAX_SIZE);
  }
}

等等。

然后:

ArrayClass<String> myStringArray = new ArrayClass<String>(String.class);