Array List in collections

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ale
    New Member
    • Oct 2007
    • 11

    #1

    Array List in collections

    I hav a doubt regarding the array lists......
    check out the program below....

    List l=new ArrayList();
    l.add(0,"hell") ;
    l.add(1,"hello" );
    l.add(2,"hello" );
    l.add(3,"hello" );
    l.add(4,"hello" );
    System.out.prin tln("Index: "+(l.indexOf("h ello")));
    System.out.prin tln("Index: "+(l.indexOf("h ello")));

    OUTPUT:index:1
    index:1

    my doubt is.....evrytime I get the index of the 1st object......wha t if I want 2 get the index of the duplicate objects other than the 1st and the last objects....??
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Ale
    I hav a doubt regarding the array lists......
    check out the program below....

    List l=new ArrayList();
    l.add(0,"hell") ;
    l.add(1,"hello" );
    l.add(2,"hello" );
    l.add(3,"hello" );
    l.add(4,"hello" );
    System.out.prin tln("Index: "+(l.indexOf("h ello")));
    System.out.prin tln("Index: "+(l.indexOf("h ello")));

    OUTPUT:index:1
    index:1

    my doubt is.....evrytime I get the index of the 1st object......wha t if I want 2 get the index of the duplicate objects other than the 1st and the last objects....??
    Yep, unlike the String class Lists don't have two parameter indefOf() method; a
    bit inconsistent, but once you've found a first index, you can update an 'offset'
    and find the other elements on a sublist of that List and iterate on it again.
    There also is a lastIndexOf() method if you need it.

    kind regards,

    Jos

    Comment

    • Ale
      New Member
      • Oct 2007
      • 11

      #3
      Originally posted by JosAH
      Yep, unlike the String class Lists don't have two parameter indefOf() method; a
      bit inconsistent, but once you've found a first index, you can update an 'offset'
      and find the other elements on a sublist of that List and iterate on it again.
      There also is a lastIndexOf() method if you need it.

      kind regards,

      Jos
      what if i dot want 2 update the 1st one....but all the other duplicate objects...?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Ale
        what if i dot want 2 update the 1st one....but all the other duplicate objects...?
        Then you should use a loop to through the ArrayList.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          Then you should use a loop to through the ArrayList.
          Loops are soooo Pascalian ;-) Recursion is our friend here:

          [code=java]
          private void update(List list, Object old, Object subst) {
          int index= list.indexOf(ol d);
          if (index >= 0) {
          list.set(index, subst);
          update(list.sub list(index+1, list.length());
          }
          }
          [/code]

          kind regards,

          Jos ;-)

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by JosAH
            Loops are soooo Pascalian ;-) Recursion is our friend here:

            [code=java]
            private void update(List list, Object old, Object subst) {
            int index= list.indexOf(ol d);
            if (index >= 0) {
            list.set(index, subst);
            update(list.sub list(index+1, list.length());
            }
            }
            [/code]

            kind regards,

            Jos ;-)
            Ah, so neat.
            And it does cater for the case when old and subst are the same.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by r035198x
              Ah, so neat.
              And it does cater for the case when old and subst are the same.
              Sure, and a smart compiler can even remove the tail recursion; if it does it
              effectively turns the recursive version into an iterative version automagically ;-)

              kind regards,

              Jos

              Comment

              Working...