• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

Iterator problem, using arrays

I'm trying to creat a main method that will crate an array with size 5 the values of the elements are {4, 7, 3, 0, 1}.
This next part i'm getting a little confused about. I'm trying to creat an iterator for the array and print the array using the iterator.

The errors i'm getting are:
2 Errors found:
Error(1): package v does not exist  ---------->( found on this line  [Iterator iter = new v.iterator();] )
Error(2): cannot find symbol  ---------------->( found on this line   [System.out.println(iter.current);] )
symbol  : variable current
location: interface java.util.Iterator

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.AbstractCollection;

public class BSTOrderedList<T extends Comparable<? super T>>
  extends AbstractCollection<T>
  implements OrderedList<T> {
 
  private BinarySearchTreeWithRank<T> bst = new BinarySearchTreeWithRank<T>();
 
  public BSTOrderedList() {
  }
 
  public int size() {
    return bst.size();
  }
 
  public boolean contains(Object x) {
    if(bst.find((T)x).equals(x))
      return true;
    else
      return false;
  }
 
  public boolean add( T x ) {
    bst.insert(x);
    if(bst.find(x).equals(x))
      return true;
    else
      return false;
  }
 
  public boolean remove(Object x) {
    bst.remove((T)x);
    if(bst.find((T)x) != x)
    return true;
    else
      return false;
  }
 
  public T get( int i ) throws NoSuchElementException {
        return bst.findKth(i);
  }
 
  public int indexOf( Object obj ) {
    if(bst.find((T)obj).equals(obj))
      return bst.indexOf((T)obj);
    else
      return -1;
  }
 
  public Iterator<T> iterator() {
    MyIterator iter = new MyIterator();
    return iter;
  }
 
  private class MyIterator implements Iterator {
     private Comparable current;
     private int k;
     
     public MyIterator() {
          current = bst.findKth(++k);
     }
     
     public boolean hasNext() {
          return bst.findKth(k + 1) != null;
     }
     
     public Comparable next() {
       return (current = bst.findKth(++k));
     }
     
     public void remove() {
       bst.remove((T)current);
     }
   
  }
 
  public static void main(String[] args) {

    int[] arr = {4, 7, 3, 0, 1};
   
  BSTOrderedList v = new BSTOrderedList();
  Iterator iter = new v.iterator();
 
while( iter.hasNext())
  System.out.println(iter.current);
  }
}
0
no158
Asked:
no158
  • 3
2 Solutions
 
hoomanvCommented:
> iter.current

Iterator does not have such filed
0
 
BogoJokerCommented:
Hi no158,

Did you mean:
System.out.println(iter.next());

Joe P
0
 
hoomanvCommented:
> new v.iterator();
eliminate new keyword
0
 
hoomanvCommented:
Iterator iter = v.iterator();
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now