Java中的泛型数组方法类

Java中的泛型数组方法类,java,arrays,generics,Java,Arrays,Generics,我一直在尝试将这个通用的arraylist类转换为一个数组,但一直未能使其正常工作。我在push()和pop()方法上遇到了障碍。感谢您的帮助 以下是原始类: public class GenericStack<E> { private java.util.ArrayList<E> list = new java.util.ArrayList<E>(); public int getSize() { return list.size();

我一直在尝试将这个通用的arraylist类转换为一个数组,但一直未能使其正常工作。我在push()和pop()方法上遇到了障碍。感谢您的帮助

以下是原始类:

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();
  }
}
public类GenericStack{
private java.util.ArrayList=new java.util.ArrayList();
公共int getSize(){
返回list.size();
}
公共E peek(){
返回list.get(getSize()-1);
}
公共电子推送(EO){
列表。添加(o);
返回o;
}
公共E-pop(){
eo=list.get(getSize()-1);
list.remove(getSize()-1);
返回o;
}
公共布尔值为空(){
return list.isEmpty();
}
}
这是我到目前为止修改的课程:

public class GenericStack<E> {
    public static int size = 16;
    @SuppressWarnings("unchecked")
    private E[] list = (E[])new Object[size];

  public void add(int index, E e) {
      ensureCapacity();

      for (int i = size - 1; i >= index; i--) {
          list[i + 1] = list[i];

      list[index] = e;

      size++;   
    }
  }
  public int getLength() {
    return list.length;
  }

  public E peek() {
      E o = null;
      o = list[0];
      return o;
  }
  public E push(E o) {
      ensureCapacity();
      list.append(o);
        size++;
        return o;
  }
  public E pop() {
      E o = null;
      for (int i = 0; i > list.length; i++) {
          o = list[i - 1];
    }
        list[list.length - 1] = null;
        size--;
        return o;
      }
  private void ensureCapacity() {
      if (size >= list.length) {
        @SuppressWarnings("unchecked")
        E[] newlist = (E[])(new Object[size * 2 + 1]);
          System.arraycopy(list, 0, newlist, 0, size);
          list = newlist;
      }
  }
  public boolean isEmpty() {
      if (list.length > 0) {
        return false;
      }
      else {
          return true;
      }
   }
}
public类GenericStack{
公共静态int size=16;
@抑制警告(“未选中”)
私有E[]列表=(E[])新对象[大小];
公共无效添加(整数索引,E){
保证再产能();
对于(inti=size-1;i>=index;i--){
列表[i+1]=列表[i];
列表[索引]=e;
大小++;
}
}
公共整数getLength(){
返回列表长度;
}
公共E peek(){
EO=零;
o=列表[0];
返回o;
}
公共电子推送(EO){
保证再产能();
列表。附加(o);
大小++;
返回o;
}
公共E-pop(){
EO=零;
对于(int i=0;i>list.length;i++){
o=列表[i-1];
}
list[list.length-1]=null;
大小--;
返回o;
}
私人无效保证{
如果(大小>=列表长度){
@抑制警告(“未选中”)
E[]新列表=(E[])(新对象[size*2+1]);
System.arraycopy(列表,0,新建列表,0,大小);
列表=新列表;
}
}
公共布尔值为空(){
如果(list.length>0){
返回false;
}
否则{
返回true;
}
}
}

NB:您必须首先更正注释中提到的代码


  • 建议使用官方堆栈类的name方法,因此有5种方法:
    empty()
    peek()
    pop()
    push(E项)
    搜索(对象o)

  • 您应该将数组的初始大小声明为常量,将当前大小声明为其他变量,并且所有属性都应该是私有的,如下所示:

    private final int MAX_SIZE = 16;
    private int currentSize=0;
    
peek()方法的代码:

public E peek() {
      E o = null;
      o = list[currentSize-1];
      return o;
}
 public E push(E o) {
  list[currentSize]=o;
  currentSize++;
    return o;
public boolean empty() {
  if (currentSize > 0) {
    return false;
  }
  else {
      return true;
    }
   }
有推送(EO)方法的代码:

public E peek() {
      E o = null;
      o = list[currentSize-1];
      return o;
}
 public E push(E o) {
  list[currentSize]=o;
  currentSize++;
    return o;
public boolean empty() {
  if (currentSize > 0) {
    return false;
  }
  else {
      return true;
    }
   }
}

pop()方法的代码。此方法必须抛出
EmptyStackException
-如果此堆栈为空:

 public E pop() {
  E o = null;
  if(currentSize>0){
      o=list[currentSize - 1];
    list[currentSize - 1] = null;
    currentSize--;
    return o;
  }else{
      throw new EmptyStackException();
  }

  }
empty()方法的代码:

public E peek() {
      E o = null;
      o = list[currentSize-1];
      return o;
}
 public E push(E o) {
  list[currentSize]=o;
  currentSize++;
    return o;
public boolean empty() {
  if (currentSize > 0) {
    return false;
  }
  else {
      return true;
    }
   }

首先,数组没有
append()
方法。您必须指定适当的索引,如:
list[size++]=oo=列表[size-1];列表[--size]=null;返回o就可以了。请注意,基本
列表
数组的长度不是GenericStack的大小。建议使用官方
堆栈
类的名称方法,因此有5种方法:
empty()
peek()
pop()
推送(E项)
搜索(对象o)