【问题标题】:Generic Array Method Class in JavaJava中的通用数组方法类
【发布时间】:2015-11-28 21:03:33
【问题描述】:

我一直在尝试将这个泛型 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 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;
      }
   }
}

【问题讨论】:

  • 对于初学者来说,数组没有append() 方法。您必须分配给适当的索引,例如:list[size++] = o;
  • 如果你知道大小,你就不必遍历你的列表。 o = list[size-1]; list[--size] = null; return o; 可以。请注意,底层 list 数组的长度不是 GenericStack 的大小。
  • 推荐使用官方Stack类的name方法,所以有5种方法:empty()peek()pop()push(E item)search(Object o)

标签: java arrays generics


【解决方案1】:

注意:您必须首先更正您的代码,如 cmets 中所述。

  • 推荐使用Stack官方类的name方法,有5种方法:empty()peek()pop()push(E item)search(Object o)

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

    private final int MAX_SIZE = 16;
    private int currentSize=0;
    

peek()方法的代码:

public E peek() {
      E o = null;
      o = list[currentSize-1];
      return o;
}

push(E o)方法的代码:

 public E push(E o) {
  list[currentSize]=o;
  currentSize++;
    return o;

}

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 boolean empty() {
  if (currentSize > 0) {
    return false;
  }
  else {
      return true;
    }
   }

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 2017-07-09
    相关资源
    最近更新 更多