3

所以我是一个大学生只是想寻求一点帮助和理解,我有一个教授不允许我们使用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;
    }
}
4

3 回答 3

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. 长度检查应Arrays.copy考虑
  2. 你可以阅读一篇关于泛型的好文章
于 2012-10-31T15:52:05.510 回答
1

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

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

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;
    }
    ...
}
于 2012-10-31T16:47:38.727 回答
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);
于 2012-10-31T16:30:30.780 回答