我正在尝试迭代对象数组。使用 next() 方法有效,所以我猜测我的迭代器类和构造函数正在工作。
由于某种原因,当 hasNext() 方法运行时我没有得到任何输出。
Iterator it = hej.iterator();
Object j = it.next();
System.out.println(j);
while(it.hasNext()){
Object i = it.next();
System.out.println(i + " ");
}
“hej”是我的对象数组。
我的 next() 代码;和hasNext()方法如下:
public class StackIterator implements Iterator<Object>{
// fields
private int element = 0;
private final Object[] elements;
private final int max;
// constructor
public StackIterator(Object[] values, int maxIndex) {
elements = values;
max = maxIndex;
}
// methods
public boolean hasNext() {
return element < max;
}
public Object next() {
return elements[element++];
}
}
构造Object Array的文件以及Object Array依赖的一个接口(interface):
public interface Stack {
int size();
boolean isEmpty();
void push(Object element);
Object pop();
Object peek();
Iterator<Object> iterator();
}
然后在另一个文件中解释这些方法:
public class StackExample implements Stack {
// fields
int length = 0;
Object[] arr;
// constructor
public StackExample() {arr = new Object[length];}
// method returns size of object array
public int size() {
return arr.length;
}
// method checks if object is empty
public boolean isEmpty() {
boolean result = false;
if (arr.length == 0){
result = true;
}
return result;
}
// method for push
public void push(Object element) {
newBiggerObj();
arr[0] = element;
}
// returns the first object of the stack
public Object pop() {
Object[] temp = new Object[arr.length-1];
Object first = arr[0];
for (int i = 0; i<arr.length-1; i++){
temp[i] = arr[i+1];
}arr = temp;
return first;
}
// returns the object on top of stack
public Object peek() {
if (isEmpty()){
try{
throw new Exception("Stack empty, can't peek!");
}
catch(Exception e){
return e.getMessage();
}
}
else {
Object first = arr[0];
return first;
}
}
// method for push method
private void newBiggerObj(){
Object[] temp = new Object[arr.length+1];
for (int i = 0; i<arr.length; i++){
temp[i+1] = arr[i];
}
arr = temp;
}
public String toString(){
String str = "";
for (int i = 0; i < arr.length; i++){
str = str + arr[i] + " , ";
}return str;
}
public Iterator<Object> iterator() {
return new StackIterator(arr, length);
}
}
令我困扰的是 Iterator 方法本身返回 Stack Iterator 类的实例。我在上面发布的。所以我真正的问题似乎是我的字段没有被赋予任何值,因为我自己没有在构造函数中给出任何值。
我测试所有这些的主要方法如下:
public class Teststack {
public static void main(String[] args){
// new instane of class StackExample
StackExample hej = new StackExample();
// test for the different methods
System.out.println(hej.isEmpty());
System.out.println(hej.size());
hej.push(4);
hej.push("hej");
hej.push(6);
hej.push(5);
System.out.println(hej.size());
System.out.println(hej.peek());
System.out.println(hej.pop());
System.out.println(hej.toString());
System.out.println(hej.isEmpty());
System.out.println("Testing Iterator: ");
// test for iterator
Iterator it = hej.iterator();
Object j = it.next();
System.out.println(j);
while(it.hasNext()){
Object i = it.next();
System.out.println(i + " ");
}
}
}
最佳答案
在您的 StackExample
类中,我没有看到在推送或弹出元素时更新 length
变量。因此,length
将始终为 0,并且对 it.hasNext()
的调用将始终返回 false。
您不需要将长度作为单独的参数传递。您可以在 StackIterator 构造函数中找到数组的长度并使用它。
另请注意,由于您在每次推送和弹出时创建一个新数组,因此 StackExample#iterator()
返回的迭代器将在每次推送/弹出后变得过时,因为它将在堆栈的旧副本/状态。
关于java - 迭代器堆栈 hasNext() 不返回 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27862956/