java吧 关注:1,226,323贴子:12,683,469
  • 2回复贴,共1

有关迭代器iterator的java实现的问题,求解答

取消只看楼主收藏回复

public Iterator<Item> iterator(){ return new ArrayIterator();}private class ArrayIterator implements Iterator<Item>{ private int[] random; private int current = 0; //line: 47 random = new int[N]; for(int i = 0; i < N; i++) random[i] = head + i; StdRandom.shuffle(random); public boolean hasNext(){ //line:52 return current != (N - 1); } public void remove(){ } public Item next(){ return RQ[random[current++]]; }}
错误报告
File: C:\Users\CHN\algs4\RandomizedQueue.java [line: 47]
Error: Syntax error on token ";", { expected after this token
File: C:\Users\CHN\algs4\RandomizedQueue.java [line: 52]
Error: Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration
File: C:\Users\CHN\algs4\RandomizedQueue.java [line: 52]
Error: Syntax error, insert ";" to complete LocalVariableDeclarationStatement
File: C:\Users\CHN\algs4\RandomizedQueue.java [line: 52]
Error: Syntax error on token "{", ; expected
自己写的,一个练习。总报告缺分号、花括号之类的,但明明已经配对了。
但像这样的又可以( 不是同一个类里的):
public Iterator iterator(){
return new ListIterator();
}
private class ListIterator implements Iterator{
private Node current = first;
public boolean hasNext(){
return current != null;
}
public void remove(){ }
public Item next(){
Item item = current.item;
current = current.next;
return item;
}
}
对java一知半解,求解答,谢谢。


IP属地:广西1楼2015-03-10 19:37回复
    新人求教啊
    毕竟要实现一个特殊队列


    IP属地:广西3楼2015-03-10 19:40
    回复
      //所有代码,有两个函数没写
      /**
      * Auto Generated Java Class.
      * 2015.3.7
      */
      import java.util.Iterator;
      public class RandomizedQueue<Item> implements Iterable<Item>{
      private Item[] RQ;
      private int N;
      private int head;
      private int tail;
      public RandomizedQueue (){
      RQ = (Item[]) new Object[1];
      N = 0;
      head = 0;
      tail = 0;
      }
      public boolean isEmpty(){
      return N == 0;
      }
      public int size(){
      return N;
      }
      public void enqueue(Item item){
      if(N == RQ.length)
      resize(2*RQ.length);
      RQ[++tail] = item;
      N++;
      }
      public Item dequeue(){
      int RN;
      RN = StdRandom.uniform(head, tail);
      RQ[RN] = null;
      move(RN);
      if(N > 0 && N == RQ.length/4)
      resize(RQ.length);
      return RQ[RN];
      }
      public Item sample(){
      return RQ[StdRandom.uniform(head, tail)];
      }
      public Iterator<Item> iterator(){
      return new ArrayIterator();
      }
      private class ArrayIterator implements Iterator<Item>{
      private int[] random;
      private int current = 0;
      random = new int[N];
      for(int i = 0; i < N; i++)
      random[i] = head + i;
      StdRandom.shuffle(random);
      public boolean hasNext(){
      return current != (N - 1);
      }
      public void remove(){ }
      public Item next(){
      return RQ[random[current++]];
      }
      }
      private void move(int x){
      }
      private void resize(int max){
      }
      }


      IP属地:广西4楼2015-03-10 19:42
      回复