no158
asked on
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.c urrent);] )
symbol : variable current
location: interface java.util.Iterator
import java.util.Iterator;
import java.util.NoSuchElementExc eption;
import java.util.AbstractCollecti on;
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.cu rrent);
}
}
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.c
symbol : variable current
location: interface java.util.Iterator
import java.util.Iterator;
import java.util.NoSuchElementExc
import java.util.AbstractCollecti
public class BSTOrderedList<T extends Comparable<? super T>>
extends AbstractCollection<T>
implements OrderedList<T> {
private 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
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.cu
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> new v.iterator();
eliminate new keyword
eliminate new keyword
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Iterator does not have such filed