不接受Java中的参数
我从以下代码中收到错误“类型堆栈不接受参数公共类ArrayStack实现堆栈”:不接受Java中的参数,java,class,parameters,implements,Java,Class,Parameters,Implements,我从以下代码中收到错误“类型堆栈不接受参数公共类ArrayStack实现堆栈”: public class ArrayStack<E> implements Stack<E> { private E[] data; private int size; public ArrayStack() { data = (E[])(new Object[1]); size = 0; } public boolean isEmpty() { return size == 0;
public class ArrayStack<E> implements Stack<E> {
private E[] data;
private int size;
public ArrayStack() {
data = (E[])(new Object[1]);
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public Object pop() {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return data[size];
}
public Object peek() {
if (isEmpty()) {
throw new EmptyStructureException();
}
return data[size - 1];
}
protected boolean isFull() {
return size == data.length;
}
public void push(Object target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]);
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}
公共类ArrayStack实现堆栈{
私人电子数据;
私有整数大小;
公共阵列堆栈(){
数据=(E[])(新对象[1]);
尺寸=0;
}
公共布尔值为空(){
返回大小==0;
}
公共对象pop(){
if(isEmpty()){
抛出新的EmptyStructureException();
}
大小--;
返回数据[大小];
}
公共对象peek(){
if(isEmpty()){
抛出新的EmptyStructureException();
}
返回数据[size-1];
}
受保护的布尔值isFull(){
返回大小==data.length;
}
公共无效推送(对象目标){
如果(isFull()){
拉伸();
}
数据[大小]=目标;
大小++;
}
受保护的空拉伸(){
E[]newData=(E[])(新对象[data.length*2]);
对于(int i=0;i
“类型堆栈不接受参数公共类ArrayStack实现堆栈”
堆栈类如下所示
public interface Stack<E> {
public boolean isEmpty();
public E peek();
public E pop();
public void push(E target);
}
公共接口堆栈{
公共布尔值为空();
公共E peek();
公共E pop();
公共无效推送(E目标);
}
您的peek()方法应该是这样的
public E peek() throws EmptyStructureException {
if (isEmpty()) {
throw new EmptyStructureException();
}
return (E)data[size - 1];
}
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
public E pop() throws EmptyStructureException {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return (E)data[size];
}
您的push()方法应该是这样的
public E peek() throws EmptyStructureException {
if (isEmpty()) {
throw new EmptyStructureException();
}
return (E)data[size - 1];
}
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
public E pop() throws EmptyStructureException {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return (E)data[size];
}
您的pop()方法应该如下所示
public E peek() throws EmptyStructureException {
if (isEmpty()) {
throw new EmptyStructureException();
}
return (E)data[size - 1];
}
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
public E pop() throws EmptyStructureException {
if (isEmpty()) {
throw new EmptyStructureException();
}
size--;
return (E)data[size];
}
现在您的界面如下所示
public interface Stack<E> {
public boolean isEmpty();
public E peek() throws EmptyStructureException;
public E pop() throws EmptyStructureException;
public void push(E target);
}
公共接口堆栈{
公共布尔值为空();
public E peek()抛出EmptyStructureException;
public E pop()抛出EmptyStructureException;
公共无效推送(E目标);
}
不确定错误是什么,但ArrayStack中的pop和peek应返回E,而push方法应为E。为什么要重新发布?通常,当人们重新发布完全相同的问题时,通常意味着旧问题的答案为0。我不明白,你的另一个问题有两个答案。这是一个完全不同的问题。试着通读一遍,而不仅仅是看代码。