array to iterator

R

Roedy Green

Is there a more efficient way to turn an array of Strings into an
Iterator of Strings?

FilenameFilter f = new ClamFilter( "", ".html" );
String [] fileNames = new File( "E:/mindprod/ggloss" ).list( f );
ArrayList toProcess = new ArrayList( Arrays.asList( fileNames) );
Iterator fileIterator = toProcess.iterator();
 
M

Mike Schilling

Roedy Green said:
Is there a more efficient way to turn an array of Strings into an
Iterator of Strings?

FilenameFilter f = new ClamFilter( "", ".html" );
String [] fileNames = new File( "E:/mindprod/ggloss" ).list( f );
ArrayList toProcess = new ArrayList( Arrays.asList( fileNames) );
Iterator fileIterator = toProcess.iterator();

Why create the intermediate ArrayList?

Iterator fileIter = Arrays.asList().iterator();

It wouldn't be hard to create an Iterator that processes the array directly,
but why bother?
 
J

John C. Bollinger

Roedy said:
Is there a more efficient way to turn an array of Strings into an
Iterator of Strings?

Efficient in terms of source code? No, no better than you already had.
Efficient with respect to creating objects, copying references around,
etc.? For that, consider the following:
FilenameFilter f = new ClamFilter( "", ".html" );
final
String [] fileNames = new File( "E:/mindprod/ggloss" ).list( f );

Iterator fileIterator = new Iterator() {
private int position = 0;

public boolean hasNext() {
return (position < fileNames.length);
}

public Object next() {
if (hasNext()) {
return fileNames[position++];
} else {
throw new NoSuchElementException();
}
}

public void remove() {
throw new UnsupportedOperationException();
}
};


John Bollinger
(e-mail address removed)
 
B

Boudewijn Dijkstra

Roedy Green said:
Is there a more efficient way to turn an array of Strings into an
Iterator of Strings?

FilenameFilter f = new ClamFilter( "", ".html" );
String [] fileNames = new File( "E:/mindprod/ggloss" ).list( f );
ArrayList toProcess = new ArrayList( Arrays.asList( fileNames) );
Iterator fileIterator = toProcess.iterator();

How about an ArrayIterator?


import java.util.*;

/**
* Iterator for iterating over an array of <code>Object</code>s.
* The {@link #remove()} and {@link #add(Object)} methods are unsupported,
* use an ArrayList for these operations.
*/
public class ArrayIterator
implements ListIterator
{
/** The array over which is being iterated. */
protected Object[] array;
/** The length of the array is buffered here. */
protected int length;
/**
* This member is used to keep track of the cursor of the iterator.
* When iterating, it is used to obtain an index into the array.
*/
protected int cursor;
/** The index of the last returned element is stored here, for use in
set(Object). */
protected int lastIndex;


public ArrayIterator(Object[] array)
{
this.array = array;
length = array.length;
cursor = -1; // don't know initial direction
lastIndex = -1;
}


public boolean hasNext()
{
if (cursor == -1)
return length > 0;
return cursor < length;
}

public Object next()
{
int cursor = this.cursor;
if (cursor == -1)
cursor = 0;
try {
Object o = array[cursor];
lastIndex = cursor;
this.cursor = cursor + 1; // post-increment
return o;
} catch (ArrayIndexOutOfBoundsException aioobe) {
NoSuchElementException rethrow = new
NoSuchElementException("ArrayIndexOutOfBoundsException");
rethrow.initCause(aioobe);
throw rethrow;
}
}

public boolean hasPrevious()
{
if (cursor == -1)
return length > 0;
return cursor > 0;
}

public Object previous()
{
int cursor = this.cursor;
if (cursor == -1)
cursor = length;
try {
Object o = array[--cursor]; // pre-decrement
lastIndex = cursor;
this.cursor = cursor;
return o;
} catch (ArrayIndexOutOfBoundsException aioobe) {
NoSuchElementException rethrow = new
NoSuchElementException("ArrayIndexOutOfBoundsException");
rethrow.initCause(aioobe);
throw rethrow;
}
}

public int nextIndex() {
return lastIndex + 1;
}

public int previousIndex() {
return lastIndex - 1;
}

// unsupported
public void remove() {
throw new UnsupportedOperationException("Size of array is fixed");
}

public void set(Object o)
{
try {
array[lastIndex] = o;
} catch (ArrayIndexOutOfBoundsException aioobe) {
IllegalStateException rethrow = new IllegalStateException("Neither next()
nor previous() have been called");
rethrow.initCause(aioobe);
throw rethrow;
}
}

// unsupported
public void add(Object o) {
throw new UnsupportedOperationException("Size of array is fixed");
}
}


For the die-hards I also have a PrimitiveArrayIterator, that iterates over
any array with primitive-typed elements. But that has nothing to do with
efficiency. :)
 
M

Mike Schilling

Roedy Green said:
This method belong is the Arrays class.

Why, when Arrays.asList(arr).iterator works perfectly, and is nicely
orthogonal: first make a list, then apply normal list operations to it.

If you're concerned about the cost of creating one extra object, you need to
get a life.
 
T

Tony Morris

Roedy Green said:
Is there a more efficient way to turn an array of Strings into an
Iterator of Strings?

import java.util.Iterator;

// Why you would do this I don't know!?
class X
{
public static void main(final String[] args)
{
Iterator i = new Iterator()
{
int counter = 0;

public boolean hasNext()
{
return counter < args.length;
}

public Object next()
{
return args[counter++];
}

public void remove()
{
throw new UnsupportedOperationException("An array cannot have elements
removed");
}
};

while(i.hasNext())
{
System.out.println(i.next());
}
}
}

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
R

Roedy Green

Iterator i = new Iterator()
{
int counter = 0;

one reason you might want to do this is you need an iterator, and you
want to add a little filtering. The iterator can filter more
efficiently than you can remove elements from a collection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top