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

Iterating through an array

How would you accomplish this using this block of code

  public static void main(String[] args) {

    int[] arr = {4, 7, 3, 0, 1};
   
  BSTOrderedList v = new BSTOrderedList();
  Iterator iter = v.iterator();
 
  while(iter.hasNext())
  System.out.println(iter.next());
  }
0
no158
Asked:
no158
  • 3
1 Solution
 
CEHJCommented:
Where is this class BSTOrderedList?
0
 
no158Author Commented:
So I'm trying to creat a simple main method that will creat an array and use the MyIterator class which is nested within BSTOrderedList. The the object here is to output the information of the array through the costom iterator.

/* BSTOrderedList.java
 */
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);
     }


Links to other files that may be requested:
http://www-inst.eecs.berkeley.edu/~cs47b/weiss/nonstandard/BinarySearchTreeWithRank.java
http://www-inst.eecs.berkeley.edu/~cs47b/weiss/nonstandard/BinarySearchTree.java
http://www-inst.eecs.berkeley.edu/~cs47b/weiss/nonstandard/BinaryNode.java
   
  }
0
 
no158Author Commented:
Note: the links provided should not contain "package weiss.nonstandard;"
0
 
objectsCommented:
looks like you want to first add the array to the list

  int[] arr = {4, 7, 3, 0, 1};
   
  BSTOrderedList v = new BSTOrderedList();
   for (int i=0; i<arr.length; i++) {
     v.add(arr[i]);
   }
  Iterator iter = v.iterator();
  while(iter.hasNext())
  System.out.println(iter.next());
  }
0
 
no158Author Commented:
yup thats it, thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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