Java中的不兼容类型错误 [英] Incompatible Types Error in Java

查看:108
本文介绍了Java中的不兼容类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到一个错误,指出存在不兼容的类型。我直接从一本书中复制了这本书,因为我们应该修改代码以增强战争的游戏。我有所有其他类完成和编译,但这一个是给我合适。下面是代码:

  public class ArrayStack< E>实现Stack< {

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 newData [i] = data [i];
}
data = newData;
}
}

错误发生在push数据[size] =目标;



EDIT :::
我现在收到此错误。
类型栈不接受参数
public class ArrayStack implements Stack



堆栈类如下。

  public interface Stack< E> {

public boolean isEmpty();

public E peek();

public E pop();

public void push(E target);

}


解决方案

code> push()方法的参数类型将code>对象更改为 E

  public void push(E target){
if(isFull()){
stretch ;
}
data [size] = target;
size ++;
}

同样,您也应该更改 pop()和 peek() E

  public E pop(){
if(isEmpty()){
throw new EmptyStructureException
}
size--;
return data [size];
}

public E peek(){
if(isEmpty()){
throw new EmptyStructureException();
}
return data [size - 1];
}

p>

I keep receiving an error that says that there are incompatible types. I copied this directly out of a book because we are supposed to make changes to the code to enhance the game of War. I have all of the other classes complete and compiled but this one is giving me fits. Here is the code:

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;
}   
}

The error is occurring in the push() method at the data[size] = target; line.

EDIT::: I'm now receiving this error. "type Stack does not take parameters public class ArrayStack implements Stack"

The stack class is as follows.

public interface Stack<E> {

public boolean isEmpty();

public E peek();

public E pop();

public void push(E target);

}

解决方案

Change Object to E as the push() method's parameter type.

public void push(E target) {
    if (isFull()) {
        stretch();
    }
    data[size] = target;
    size++;
}

Likewise, you should also change the declare return type of pop() and peek() to E.

public E pop() {
    if (isEmpty()) {
        throw new EmptyStructureException();
    }
    size--;
    return data[size];
}

public E peek() {
    if (isEmpty()) {
        throw new EmptyStructureException();
    }
    return data[size - 1];
}

Now your class is fully generic.

这篇关于Java中的不兼容类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆