繁体   English   中英

如何在Java中将Generic Arraylist转换为Array?

[英]How to convert Generic Arraylist into Array in Java?

我需要更改此程序以使用数组而不是数组列表。 显然我需要创建一个对象数组并将其转换为 E[] 但我不明白如何做到这一点。 任何帮助表示赞赏。

public class GenericStack<E> {
  private java.util.ArrayList<E> list = new java.util.ArrayList<E>();

  public int getSize() {
    return list.size();
  }

  public E peek() {
    return list.get(getSize() - 1);
  }

  public E push(E o) {
    list.add(o);
    return o;
  }

  public E pop() {
    E o = list.get(getSize() - 1);
    list.remove(getSize() - 1);
    return o;
  }

  public boolean isEmpty() {
    return list.isEmpty();
  }
}

代替:

private java.util.ArrayList<E> list = new java.util.ArrayList<E>();

用:

private Object[] array = new Object[];

当您必须返回某个值时,将其强制转换为 E。例如:

public E get(int i) {
    return (E) array[i];
}

这是必要的,因为 Java 不允许您创建泛型类型的数组。

您可以使用以下命令来创建阵列。

private E[] arr;
private int index;

public void initialize() {
    arr = (E[]) new Object[100];
    index = 0;
}

public E push(E o) {
    try {
        arr[index++] = o;
    } catch (Exception e) {
        System.out.println("Array not enough..");
    }
    return o;
}

使用它,您也可以实现其他方法。 index可用于计算堆栈的大小。

在上面的示例中,超出数组边界时会打印错误。 相反,您可以在index == arr.length - 1时将数组复制到另一个变量中。 您可以使用以下函数来复制数组..

private void copyArray() {
    E[] tem = (E[]) new Object[arr.length + 100];
    System.arraycopy(arr, 0, tem, 0, arr.length);
    arr = tem;
}

您可以像这样初始化一个通用数组:

E[] stack = (E[])new Object[size];

我在另一个问题中找到了一个通用堆栈示例:Generic Stack Array

实际上你想要做的是使用数组的堆栈实现。

使用数组的通用堆栈实现

public class GenericStack<E> {
    private int index;
    private E[] array;

    public GenericStack(int size) {
        array = (E[]) new Object[size];
        index = 0;
    }

    public int getSize() {
        return index;
    }

    public int getCapacity() {
        return array.length;
    }

    public E peek() {
        return array[index-1];
    }

    // push MUST be void, it only pushes the value into the stack
    public void push(E o) {
        array[index++] = o;
    }


    public E pop() {
        E o = array[index-1];
        index--;

        return o;
    }

    public boolean isEmpty() {
        return index == 0 ? true : false; 
    }
}

测试代码

public class TestGenericStack {

    public static void main(String[] args) {
        GenericStack<Integer> genStack = new GenericStack<Integer>(10);

        // test size
        System.out.println("Size of the Stack  : " + genStack.getSize());

        // test isEmpty()
        System.out.println("Stack isEmpty      : " + genStack.isEmpty());


        System.out.println("*** Stack push operations ***");

        // test push
        genStack.push(new Integer(10));
        genStack.push(new Integer(20));
        genStack.push(new Integer(30));
        genStack.push(new Integer(15));
        genStack.push(new Integer(99));



        // test isEmpty()
        System.out.println("Stack isEmpty      : " + genStack.isEmpty());

        // test peek
        System.out.println("Top of the Stack   : " + genStack.peek());

        // test size
        System.out.println("Size of the Stack  : " + genStack.getSize());

        // test pop
        System.out.println("Pop from the Stack : " + genStack.pop());

        // test peek
        System.out.println("Top of the Stack   : " + genStack.peek());

        // test size
        System.out.println("Size of the Stack  : " + genStack.getSize());
    }

}

控制台输出

Size of the Stack  : 0
Stack isEmpty      : true
*** Stack push operations ***
Stack isEmpty      : false
Top of the Stack   : 99
Size of the Stack  : 5
Pop from the Stack : 99
Top of the Stack   : 15
Size of the Stack  : 4

数组不能使用泛型类型,因为它需要为类型分配内存。 因此,它需要事先知道它是哪种类型。 您可以使用 newInstance() 方法将使用 ArrayList 实现的堆栈转换为数组。 代码将如下所示。

public E[] toArray() {

    if (size == 0)
        return null;

    E temp = list.get(0);
    E[] stackArray = (E[]) Array.newInstance(temp.getClass(), size);
    for (int i = 0; i < list.size(); i++) {
        stackArray[i] = list.get(i);
    }
    return (E[]) stackArray;
}

泛型在编译时被删除,因此Helper.toArray将被编译为返回一个Object[]

对于这种特殊情况,我建议您使用 List.toArray(E[])。

List<E> list = new ArrayList<E>();
     list.add(...);
     list.add(...);

T[]array = list.toArray(new E[list.size()]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM