Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

Iterate over two lists in parallel

Thread view: 
iksrazal@terra.com.br - 06 Jul 2005 17:16 GMT
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
Jakob Bieling - 06 Jul 2005 18:29 GMT
> Hi all, this is not a homework assignment, I'm just trying to figure
> out the best way to do this.
[quoted text clipped - 37 lines]
>
> 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
Signature

jb

(reply address in rot13, unscramble first)

Roland - 06 Jul 2005 18:41 GMT
> Hi all, this is not a homework assignment, I'm just trying to figure
> out the best way to do this.
[quoted text clipped - 38 lines]
> 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));

}

Signature

Regards,

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

Owen Jacobson - 07 Jul 2005 05:10 GMT
>> I want to use valA and valB like so:
>>
[quoted text clipped - 20 lines]
> 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.
Roedy Green - 06 Jul 2005 19:34 GMT
>        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.

Signature

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.