Iterate over two lists in parallel

Discussion in 'Java' started by iksrazal@terra.com.br, Jul 6, 2005.

  1. Guest

    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
     
    , Jul 6, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Jul 6, 2005
    #2
    1. Advertising

  3. Roland Guest

    On 6-7-2005 18:16, wrote:
    > 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_/ /__/
    / \ /_/ / \
     
    Roland, Jul 6, 2005
    #3
  4. Roedy Green Guest

    On 6 Jul 2005 09:16:48 -0700, wrote or quoted :

    > 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
     
    Roedy Green, Jul 6, 2005
    #4
  5. On Wed, 06 Jul 2005 19:41:51 +0200, Roland wrote:

    > On 6-7-2005 18:16, wrote:
    >
    >> 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.
     
    Owen Jacobson, Jul 7, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,107
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,464
    runescience
    Feb 9, 2006
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    413
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. John
    Replies:
    4
    Views:
    926
    RedGrittyBrick
    Apr 1, 2008
  5. kj
    Replies:
    6
    Views:
    332
    Steven D'Aprano
    Jun 6, 2009
Loading...

Share This Page