Iterate over two lists in parallel

I

iksrazal

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
 
J

Jakob Bieling

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 .


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
 
R

Roland

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
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_/ /__/
/ \ /_/ / \
 
R

Roedy Green

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

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.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
O

Owen Jacobson

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
Since you are using List, you can use List.get(index) to access list
elements.

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.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top