如何将以下程序转换为java中的通用程序

时间:2014-04-15 16:40:05

标签: java arrays generics

将以下内容转换为Java中的通用程序。 它是数组1维度程序。我在通用程序中转换它时遇到了问题。请帮帮我..!

public class Array1D {
    protected int AS;
    protected Object[] elements;

    public Array1D(int initialCapacity) {
        if (initialCapacity < 1) {
            throw new IllegalArgumentException("initialCapacity must be >= 1");
        }
        elements = new Object[initialCapacity];
        AS = initialCapacity;
    }

    public Array1D() {
        elements = null;
        AS = 0;
    }

    public void create(int initialCapacity) {
        if (initialCapacity < 1) {
            throw new IllegalArgumentException("initialCapacity must be >= 1");
        }
        elements = new Object[initialCapacity];
        AS = initialCapacity;
    }

    void checkIndex(int index) {
        if (index < 0 || index >= AS) {
            throw new IndexOutOfBoundsException("index = " + index + "Size = " + AS);
        }
    }

    public boolean put(int index, Object theElement) {
        checkIndex(index);
        elements[index] = theElement;
        return true;
    }

    public Object get(int index) {
        checkIndex(index);
        return elements[index];
    }

    public Object remove(int index) {
        checkIndex(index);
        Object ob = elements[index];
        elements[index] = null;
        return ob;
    }

    public int indexOf(Object theElement) {
        for (int i = 0; i < AS; i++) {
            if (elements[i] == theElement) //if(elements[i].equals(theElement))
            {
                return i;
            }
        }
        return -1;
    }

    public int length() {
        return AS;
    }

    public Object[] changeLength(int newLength) {
        Object newArray[] = new Object[newLength];
        System.arraycopy(elements, 0, newArray, 0, AS);
        elements = newArray;
        AS = newLength;
        return newArray;
    }

    public String toString() {
        StringBuffer s = new StringBuffer("[");
        for (int i = 0; i < AS; i++) {
            if (elements[i] == null) {
                s.append("__, ");
            } else {
                s.append(elements[i].toString() + ", ");
            }
        }
        if (AS > 0) {
            s.delete(s.length() - 2, s.length());
        }
        s.append("]");
        return new String(s);
    }

}

数组类结束。 这是它的跑步者类。 我在数组中传递了8个元素。

public class Runner {
    static Array1D a = new Array1D(8);

    public static void main(String[] args) {
        System.out.println("Length of array is:  " + a.length());
        System.out.println("Array: " + a.toString());

        System.out.println();
        a.put(0, (Character) 'A');
        System.out.println("'A' Stored at 0, Array: " + a.toString());
        a.put(2, (Character) 'B');
        System.out.println("'B' Stored at 2, Array: " + a.toString());
        a.put(5, (Character) 'C');
        System.out.println("'C' Stored at 3, Array: " + a.toString());

        System.out.println();
        System.out.println("Output for get(0) is:  " + a.get(0));
        System.out.println("Output for get(2) is:  " + a.get(2));
        System.out.println("Output for get(6) is:  " + a.get(6));

        System.out.println();
        System.out.println("Index of a: " + a.indexOf((Character) 'B'));
        System.out.println("Index of c: " + a.indexOf((Character) 'C'));
        System.out.println("Index of q: " + a.indexOf((Character) 'q'));

        System.out.println();
        a.put(0, (Character) 'A');
        System.out.println("'A' Stored at 0, Array: " + a.toString());
        a.put(2, (Character) 'B');
        System.out.println("'B' Stored at 2, Array: " + a.toString());
        a.put(5, (Character) 'C');
        System.out.println("'C' Stored at 3, Array: " + a.toString());

        System.out.println();
        System.out.println(a.remove(0) + " removed from 0, Array: " + a.toString());
        System.out.println(a.remove(1) + " removed from 1, Array: " + a.toString());

        a.changeLength(10);

        System.out.println();
        System.out.println("Length of array is:  " + a.length());
        System.out.println("Array: " + a.toString());

        Character ch = 65;
        System.out.println();
        for (int ctr = 0; ctr < a.length(); ctr++) {
            a.put(ctr, ch);
            System.out.println("'" + ch + "'" + " Stored at " + ctr + ", Array: " + a.toString());
            ch++;
        }
    }

}

跑步者班级结束。

2 个答案:

答案 0 :(得分:1)

仿制药并不难学(只有难以掌握)。但是你走了:

public class Array1D<E> {
    protected int AS;
    protected Object[] elements;

    public Array1D( int initialCapacity){ 
        if(initialCapacity < 1)  
            throw new IllegalArgumentException ("initialCapacity must be >= 1");
        elements = new Object[initialCapacity];
        AS = initialCapacity;
    }

    public Array1D(){
        elements = null;
        AS = 0;
    }

    public void create(int initialCapacity){ 
        if(initialCapacity < 1)  
            throw new IllegalArgumentException ("initialCapacity must be >= 1");
        elements = new Object[initialCapacity];
        AS = initialCapacity;
    }

    void checkIndex(int index){
        if(index < 0 || index>=AS )
            throw new IndexOutOfBoundsException ("index = "+ index + "Size = "+ AS);
    }

    public boolean put( int index, E theElement ){
        checkIndex(index);
        elements[index] = theElement;
        return true;
    }

    public E get( int index ){
        checkIndex(index);
        return (E)elements[index];
    }

    public E remove( int index ){
        checkIndex(index);
        E ob = (E)elements[index];
        elements[index] = null;
        return ob;
    }

    public int indexOf(E theElement){
        for(int i=0;i<AS; i++)
            if(elements[i]==theElement)
            //if(elements[i].equals(theElement))
                return i;
        return -1;
    }

    public int length(){
        return AS;
    }

    public Object[] changeLength(int newLength){
        Object newArray[]= new Object[newLength];
        System.arraycopy(elements, 0, newArray, 0, AS); 
        elements=newArray;
        AS=newLength;
        return newArray;
    }

    public String toString(){ 
        StringBuffer s = new StringBuffer("[");
        for (int i = 0; i < AS; i++) 
            if(elements[i] ==null) 
                s.append("__, "); 
            else s.append(elements[i].toString() + ", "); 
        if(AS > 0) 
            s.delete(s.length() -2, s.length()); 
        s.append("]");
        return new String(s); 
    }
}

请注意,您无法分配通用数组。唯一的方法是分配Object数组并在需要时将其强制转换为E

然后,您可以使用Array1D创建Array1D<Character> a = new Array1D<>();个字符,并且它应该强制数据结构仅包含字符。

答案 1 :(得分:0)

非常简单,快速而肮脏。只需将所有方法和类成员放入另一个具有“public static void main”方法的类中。然后确保您刚刚复制的所有成员和方法都是静态的。然后,在那个main方法中,只需按照任何顺序调用同一类中的其他方法,并使用您想要的任何值。