Why is it that I keep getting the same "cannot access non-static method" error, but I end up with something else when I change it?

huangapple go评论81阅读模式
英文:

Why is it that I keep getting the same "cannot access non-static method" error, but I end up with something else when I change it?

问题

我试图研究一些队列方法,稍微练习一下,所以我从Oracle的文档中选择了一些方法并尝试了一下。我的代码中的一切似乎都在顺利运行,除了这个我一直无法解决的小问题。我在编程方面还很新手,我正在上大学学习编程,但我仍然在学习基础知识。以下是我代码的内容:

import java.util.NoSuchElementException;

public class Queue<E> {

    private Object[] queue;
    private int size = 0;
    private int top;
    private int bottom;

    public Queue() {
        queue = new Object[10];
    }

    public Queue(int capacity) {
        queue = new Object[capacity];
    }

    @SuppressWarnings("unchecked")
    public E elm() {
        if (size == 0) {
            throw new NoSuchElementException();
        }
        return (E) queue[top];
    }

    public boolean add(E elm) {
        if (elm == null) {
            throw new NullPointerException();
        }
        if (size == queue.length) {
            int newCapacity = (int) Math.ceil(queue.length * 1.5); // 修改这里的乘法符号
            Object[] newArr = new Object[newCapacity];
            for (int i = 0; i < queue.length; i++) {
                newArr[i] = queue[i];
            }
        }
        if (bottom == -1) {
            top = 0;
            bottom = 0;
            queue[bottom] = elm;
        } else {
            bottom = (bottom + 1) % queue.length;
        }
        queue[bottom] = elm;
        size++;
        return true;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    @SuppressWarnings("unchecked")
    public E remove() {
        if (size == 0) {
            throw new NoSuchElementException();
        }
        E elm = (E) queue[top];
        top = (top + 1) % queue.length;
        size--;

        if (size == 0) {
            bottom = -1;
        }
        return elm;
    }

    public void clear() {
        return;
    }

    public int size() {
        return size;
    }

    boolean contains(Object o) {
        for (int i = 0; i < size; i++) { // 修改这里的循环条件
            if (queue[i] != null && queue[i].equals(o)) {
                return true;
            }
        }
        return false;
    }
}

问题出现在最后一个代码块中。我一直在Eclipse中收到这样的消息:“无法对非静态方法contains(Object)从Queue类型进行静态引用”,它建议我将contains()更改为静态方法。我已经尝试过这样做,但没有成功。当我将它更改为静态方法后,运行时只会不断重复出现“Exception in thread "main" java.lang.StackOverflowError”错误,而且会一直指向错误的位置。我不确定我在这里做错了什么?我将在下面附上我的驱动程序;我使用它来测试我的一些代码,但它并没有太多内容。

public class Driver {

    public static void main(String[] args) {
        Queue<Integer> a1 = new Queue<>();

        a1.add(10);
        a1.add(79);

        System.out.println(a1.size());

        System.out.println(a1.contains(10));
    }
}
英文:

Im trying to work on some of the Queue methods, get some practice with them in a little bit so I chose a handful from the list on Oracle's documentation and gave them a shot. Everything in my code seems to be working smoothly except for this one hiccup that I have not been able to get over. I'm pretty new at programming and I am in college for it, but I am still learning the basics. Here is what I have for my code:

import java.util.NoSuchElementException;
public class Queue&lt;E&gt; {
private Object[] queue;
private int size = 0;
private int top;
private int bottom;
public Queue() {
queue = new Object[10];
}
public Queue(int capacity) {
queue = new Object[capacity];
}
@SuppressWarnings(&quot;unchecked&quot;)
public E elm() {
if (size == 0) {
throw new NoSuchElementException();
}
return (E) queue[top];
}
public boolean add(E elm) {
if (elm == null) {
throw new NullPointerException();
}
if (size == queue.length) {
int newCapacity = (int)Math.ceil(queue.length + 1.5);
Object[] newArr = new Object[newCapacity];
for (int i = 0; i &lt; queue.length; i++) {
newArr[i] = queue[i];
}
}
if (bottom == -1) {
top = 0;
bottom = 0;
queue[bottom]= elm;
} else {
bottom = (bottom +1) % queue.length;
}
queue[bottom] = elm;
size++;
return true;
}
public boolean isEmpty() {
return size == 0;
}
@SuppressWarnings(&quot;unchecked&quot;)
public E remove() {
if (size == queue.length) {
throw new NoSuchElementException();
}
E elm = (E) queue[top];
top = (top +1) % queue.length;
size--;
if (size == 0) {
bottom = -1;	
}
return elm;
}
public void clear() {
return;
}
public int size() {
for (int i = 0; i &lt; queue.length; i++) {
size += 1;
}
return size;
}
boolean contains(Object o) {
if (Queue.contains(o)) {
return true;
} else {
return false;
}
}
}//end of file 

the problem lies in the very last block of code. I keep getting the message in Eclipse, telling me "Cannot make a static reference to the non-static method contains(Object) from the type Queue" and it is suggesting I change contains() to static. I have tried this to no avail. When I do change it to static, I only get a repeating error upon running it that says "Exception in thread "main" java.lang.StackOverflowError" and gives the location of the error repeatedly. I am not sure what it is that I am doing wrong here? I'll put my driver below; I was using it to test some of my code but it doesnt have a whole lot in it.


public class Driver {
public static void main(String[] args) {
Queue&lt;Integer&gt; a1 = new Queue&lt;&gt;();
a1.add(10);
a1.add(79);
System.out.println(a1.size());
System.out.println(a1.contains(10));
}
}

答案1

得分: 1

Queue.contains是调用Queue类的静态方法的表示方法,这就是为什么它在抱怨。

如果将其更改为静态,那么您只是使contains函数不断递归调用自身,从而导致堆栈溢出。

您将需要自己编写代码,逐个比较每个元素以进行contains函数的判断。

英文:

Queue.contains is notation of calling a static method of the Queue class, which is why it is complaining.

On changing it to static, you are then just making the contains functions call itself recursively forever which is why it then stack overflows.

You will need to write the code yourself to compare each element for the contains function.

huangapple
  • 本文由 发表于 2020年10月28日 06:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64563983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定