英文:
when I compile, it said cannot find symbol in deque java and I also dont know why dose pushleft contain this line left = (left+1) % queue.length;
问题
public class DequeCyclic implements Deque{
private int left, right, size, capacity;
private Object[] queue;
// 构造一个空队列
// @param s 是数组的最终大小
public DequeCyclic(final int s) {
left = right = size = 0;
queue = new Object[s];
capacity = s;
}
public boolean isEmpty() {
return (size == 0);
}
public void pushLeft(Object c) throws Overflow {
if (isFull()) {
throw new Overflow("队列已满");
} else if (left == 0 && right == 0 && size == 0) {
queue[0] = c;
} else {
left = (left + 1) % queue.length;
queue[left] = c;
}
size++;
}
public static void main(String[] args) {
DequeCyclic[] ab = new DequeCyclic[10];
ab.isEmpty(); // 这是问题所在
}
}
运行时出现错误信息:
DequeCyclic.java:115: 错误: 找不到符号
ab.isEmpty();
^
符号: 方法 isEmpty()
位置: 类型为 DequeCyclic[] 的变量 ab
1 个错误
并且我也想知道为什么 pushLeft 方法有这行代码:
left = (left+1) % queue.length;
<details>
<summary>英文:</summary>
public class DequeCyclic implements Deque{
private int left, right, size, capacity;
private Object[] queue;
//Constructing an empty Queue
//@param s is the final size of the array
public DequeCyclic(final int s) {
left= right = size = 0;
queue = new Object展开收缩;
capacity = s;
}
public boolean isEmpty() {
return (size == 0);
}
public void pushLeft(Object c) throws Overflow{
if (isFull()) {
throw new Overflow ("the queue is full");
}
else if (left == 0 && right == 0 && size ==0) {
queue[0] = c;
}
else {
left = (left+1) % queue.length;
queue[left] = c;
}
size++;
}
public static void main(String[] args){
DequeCyclic[] ab = new DequeCyclic[10];
ab.isEmpty();//this is the problem
}
}
When I run this, I do not why it gave me that
DequeCyclic.java:115: error: cannot find symbol
ab.isEmpty();
^
symbol: method isEmpty()
location: variable ab of type DequeCyclic[]
1 error
And I also wanna know why does the pushLeft has this line
left = (left+1) % queue.length;
</details>
# 答案1
**得分**: 0
你的问题在这里:
`DequeCyclic[] ab = new DequeCyclic[10];`
这行代码的作用是什么?
它初始化了一个由大小为10的`DequeCyclic`组成的数组,命名为`ab`。
你可能想要的是:
`DequeCyclic ab = new DequeCyclic(10);`
你调用了`DequeCyclic`的构造函数,并将参数10传递给它,将其赋值给变量`ab`。
接下来,`left = (left+1) % queue.length;` 是什么意思?
如果`left+1 < queue.length`,那么它等同于 `left++`。
否则,一旦 `left+1 == queue.length`,由于取余运算符,left 将变为零。
<details>
<summary>英文:</summary>
Your problem is here:
`DequeCyclic[] ab = new DequeCyclic[10];`
What does this line do?
It initializes an array (size=10) of `DequeCyclic`s with the name `ab`.
What you probably want is:
`DequeCyclic ab = new DequeCyclic(10);`
You call the constructor of `DequeCyclic` with the parameter 10, assigning it to the variable `ab`.
Next, what does `left = (left+1) % queue.length;` do?
It `left+1 < queue.length`, it is equivalent to `left++`.
Else, as soon, as `left+1 == queue.length`, thanks to the modulo-operator, left will be zero.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论