Solved

Iterating through an array

Posted on 2006-05-08
275 Views
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
Question by:no158

LVL 86

Expert Comment

Where is this class BSTOrderedList?
0

Author Comment

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

Author Comment

Note: the links provided should not contain "package weiss.nonstandard;"
0

LVL 92

Accepted Solution

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++) {
}
Iterator iter = v.iterator();
while(iter.hasNext())
System.out.println(iter.next());
}
0

Author Comment

yup thats it, thanks
0

Join & Write a Comment Already a member? Login.

Suggested Solutions

Title # Comments Views Activity
canBalance challenge 34 56
Convert BufferedReader to File 1 49
Java virtual machine monitoring tools 3 30
wordappend challenge 8 47
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!