运行javac ResizingArrayStack.java
$ javac ResizingArrayStack.java
ResizingArrayStack.java:39: cannot find symbol
symbol : class item
location: class ResizingArrayStack<Item>
private class ReverseArrayIterator implements Iterator<item> {
^
ResizingArrayStack.java:53: non-static class Item cannot be referenced from a static context
ResizingArrayStack<Item> s;
^
ResizingArrayStack.java:54: non-static class Item cannot be referenced from a static context
s = new ResizingArrayStack<Item>();
^
Note: ResizingArrayStack.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
代码
import java.util.Iterator;
public class ResizingArrayStack<Item> implements Iterable<Item>
{
private Item[] a = (Item[]) new Object[1];
private int N = 0;
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
private void resize(int max) {
Item[] temp = (Item[]) new Object[max];
for (int i = 0; i < N; i++) {
temp[i] = a[i];
}
a = temp;
}
public void push(Item item) {
if (N == a.length) resize(2*a.length);
a[N++] = item;
}
public Item pop() {
Item item = a[--N];
a[N] = null;
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
public Iterator<Item> iterator() {
return new ReverseArrayIterator();
}
private class ReverseArrayIterator implements Iterator<item> {
private int i = N;
public boolean hasNext() {
return i > 0;
}
public Item next() {
return a[--i];
}
public void remove() {
}
}
public static void main(String[] args) {
ResizingArrayStack<Item> s;
s = new ResizingArrayStack<Item>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) {
s.push(item);
}
else if (!s.isEmpty()) {
StdOut.print(s.pop() + " ");
}
StdOut.println("(" + s.size() + " left on stack");
}
}
}
知道编译错误发生的原因吗?
答案 0 :(得分:1)
一些事情。首先,你似乎有一个错字
private class ReverseArrayIterator implements Iterator<item> {
^
其次,在
中使用Item
public class ResizingArrayStack<Item> implements Iterable<Item>
声明一个名为Item
的新类型变量。它相当于
public class ResizingArrayStack<T> implements Iterable<T>
如果您打算将其用作Iterable
的类型参数,请按照
public class ResizingArrayStack implements Iterable<Item>
第三,我不知道这里使用的Item
类型
ResizingArrayStack<Item> s;
被声明,但似乎是在内部类中。内部类需要外部类的实例。因此无法从static
上下文访问它。
答案 1 :(得分:-1)
你真的需要处理你的Java,很明显你来自不同的语言。以下是固定代码,可帮助您进一步理解。这不是完整的答案,但它应该足以解释您的代码有什么问题。
如果您要将其转换为Item
,则无需使用Object
的通用。
public class ResizingArrayStack implements Iterable<Object>
{
private Object[] a = new Object[1];
private int N = 0;
public boolean isEmpty() {
return N == 0;
}
public int size() {
return N;
}
private void resize(int max) {
Object[] temp = new Object[max];
for (int i = 0; i < N; i++) {
temp[i] = a[i];
}
a = temp;
}
public void push(Object item) {
if (N == a.length) resize(2*a.length);
a[N++] = item;
}
public Object pop() {
Object item = a[--N];
a[N] = null;
if (N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
public Iterator<Object> iterator() {
return new ReverseArrayIterator();
}
private class ReverseArrayIterator implements Iterator<Object> {
private int i = N;
public boolean hasNext() {
return i > 0;
}
public Object next() {
return a[--i];
}
public void remove() {
}
}
public static void main(String[] args) {
ResizingArrayStack s;
s = new ResizingArrayStack();
for (;;) {
try {
Scanner scanner = new Scanner(System.in);
String item = scanner.nextLine();
if (scanner.nextLine() != null) {
if (!item.equals("-")) {
s.push(item);
}
else if (!s.isEmpty()) {
System.out.println(s.pop() + " ");
}
}
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}