## Iterate over two lists in parallel

### Iterate over two lists in parallel

Hi all, this is not a homework assignment, I'm just trying to figure
out the best way to do this.

I have two lists of the same size, filled with Long :

useValues(List listA, List listB);

I want to iterate over each so I can do something with the values:

for (Iterator iterA = listA.iterator(); iterA.hasNext();) {
Long valA = (Long) iterA.next();

}

And

for (Iterator iterB = listB.iterator(); iterB.hasNext();) {
Long valB = (Long) iterB.next();

}

I want to use valA and valB like so:

doSomething(valA, valB);

My idea is this:

Long[] longArrayA = (Long[]) listA.toArray();
Long[] longArrayB = (Long[]) listB.toArray();

if (longArrayA.length != longArrayB.length) {
throw new IllegalArgumentException();
}

for (int xx = 0; xx < longArrayA.length; xx++) {
Long valA = longArrayA[xx];
Long valB = longArrayB[xx];
doSomething(valA, valB);
}

Any ideas? I'm using java 1.4.2 .
iksrazal

### Iterate over two lists in parallel

I do not know how expensive the toArray call is, but you could just
merge your two loops into one:

Iterator iterA = listA.iterator ();
Iterator iterB = listB.iterator ();
for (; iterA.hasNext() && iterB.hasNext (); )
{
Long valA = (Long) iterA.next ();
Long valB = (Long) iterB.next ();
doSomething (valA, valB);
}

hth
--
jb

### Iterate over two lists in parallel

Since you are using List, you can use List.get(index) to access list
elements.

int size = listA.size();
if (size != listB.size()) {
throw new IllegalArgumentException();
}
for (int i = 0; i < size; i++) {
doSomething(listA.get(i), listB.get(i));

// OR, if doSomething requires two Long arguments:
// doSomething( (Long)listA.get(i), (Long)listB.get(i));

}

--
Regards,

Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \

### Iterate over two lists in parallel

On 6 Jul 2005 09:16:48 -0700, XXXX@XXXXX.COM wrote or quoted :

Look at the old iterator for syntax:

// I T E R A T O R : alternate when you already have the Iterator
Iterator someFiles = getFilesToProcess();
while ( someFiles.hasNext() )
{
File f = (File)someFiles.next();
...
}

now just extend that to two:

// I T E R A T O R : alternate when you already have the Iterator
Iterator someFiles1 = getFilesToProcess1();
Iterator someNames2 = getNamesToProcess2();
while ( someFiles1.hasNext() && someNames2.hasNext() )
{

File f1 = (File)someFiles1.next();
String n2 = (String)someNames2.next();
...
}

It stops whichever list runs out first.

In Java 1.5 you can drop the cast.

--
Bush crime family lost/embezzled \$3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.yqcomputer.com/

See http://www.yqcomputer.com/ 's war crimes

### Iterate over two lists in parallel

Which might be O(n), if it's a linked list. Doing that in a loop with a
linked list would be.... off the top of my head, O(n*n)? Definitely not
wise unless you're sure everyone using the code will always use an
ArrayList or another list with O(1) get(int) complexity.