Trouble with arraylists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolieman
    New Member
    • Sep 2007
    • 3

    #1

    Trouble with arraylists

    Hi, Everyone, recently my programming class has started making programs involving arraylists. Our program is supposed to delete all numbers in the arraylist that contain numbers that end in the number we input and also numbers that are either +1 or -1 from the number.

    Ex.
    arrayList = [43, 59, 87, 60, 67, 25]
    What number do you wish to input?
    8
    arrayList = [43, 60, 25]

    ^ This is the output that should occur. Removes all numbers that end in 8, +1(9) and -1(7) of it.

    However, this is the real result I get. Ex.
    arrayList = [43, 59, 87, 60, 67, 25]
    What number do you wish to input?
    8
    arrayList = [43, 87, 60, 25]

    The 87 remains even though it should be gone. Does anyone know what the problem is? Here is the for loop I used to check from numbers that were equal to the number, +1, and -1 of that number.

    for (int ind = 0; ind<nbrs.size() ; ind++) {
    if (nbr > 0 || nbr < 9) {
    if ((nbrs.get(ind) %10) == (nbr) || (nbrs.get(ind) %10) == (nbr+1) || (nbrs.get(ind) %10) == (nbr-1)) {
    nbrs.remove(ind );
    }
    }
    else if (nbr == 9) {
    if ((nbrs.get(ind) %10) == nbr || (nbrs.get(ind) %10) == (nbr-1)) {
    nbrs.remove(ind );
    }
    }
    else if (nbr == 0) {
    if ((nbrs.get(ind) %10) == nbr || (nbrs.get(ind) %10) == (nbr+1)) {
    nbrs.remove(ind );
    }
    }
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    When you remove an item, you aren't doing anything to change the index. Suppose you have two elements in a row to remove (let's say at indexes 3 and 4). When your loop gets to array[3], it removes the item as is expected. But now the value that was in array[4] has moved to array[3], so you should check that spot again, yes? But, as your code stands, your loop will continue with checking the element in array[4].

    Every time you remove an item, you should set the index back 1 again, so that it will check the same spot again (which is occupied by a new value).

    Comment

    • coolieman
      New Member
      • Sep 2007
      • 3

      #3
      Thanks for the tip Ganon. I added an "ind--" into all 3 of the if statements and did the trick.

      Now lets say that I want to make the program duplicate all values that are even.

      Ex.

      arraylist = [ 24, 25, 44, 22, 12 ]

      would be turned into

      arraylist = [ 24, 24, 25, 44, 44, 22, 22, 12, 12 ]

      Code:
      for (int ind = 0; ind<nbrs.size(); ind++) {
                  if (nbrs.get(ind) %2 == 0) {
                      nbrs.add(ind+1,nbrs.get(ind));
                  }
              }
              System.out.println("nbrs = "+nbrs);
      Although the program compiles fine, I get an error message stating "OutOfMemoryErr or: Java heap space (in java.util.Array s)" when I try to run. Do you have any suggestions on how to work around this?

      Thanks

      EDIT: Got it fixed. The problem was that it went in an endless loop. Adding an ind++ in the if statement fixed the problem.

      Comment

      • kreagan
        New Member
        • Aug 2007
        • 153

        #4
        Originally posted by coolieman
        Thanks for the tip Ganon. I added an "ind--" into all 3 of the if statements and did the trick.

        Now lets say that I want to make the program duplicate all values that are even.

        Ex.

        arraylist = [ 24, 25, 44, 22, 12 ]

        would be turned into

        arraylist = [ 24, 24, 25, 44, 44, 22, 22, 12, 12 ]

        Code:
        for (int ind = 0; ind<nbrs.size(); ind++) {
                    if (nbrs.get(ind) %2 == 0) {
                        nbrs.add(ind+1,nbrs.get(ind));
                    }
                }
                System.out.println("nbrs = "+nbrs);
        Although the program compiles fine, I get an error message stating "OutOfMemoryErr or: Java heap space (in java.util.Array s)" when I try to run. Do you have any suggestions on how to work around this?

        Thanks

        EDIT: Got it fixed. The problem was that it went in an endless loop. Adding an ind++ in the if statement fixed the problem.
        lol.

        Just wondering, do you use a debugger?

        Comment

        • coolieman
          New Member
          • Sep 2007
          • 3

          #5
          Originally posted by kreagan
          lol.

          Just wondering, do you use a debugger?
          No. I'm just a student and I use BlueJ as my compiler. Why do you ask though?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Perhaps a debugger would be catching these problems before they compiled...

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              I'm jumping in a bit late but nevertheless: I hate all those complicated if-clauses
              inside the body of a loop; IMHO, the loop should look like this:

              [code=java]
              for (int i= 0, i < list.length(); i++)
              if (removePredicat e(list.get(i))
              list.remove(i--);
              [/code]

              The navigation is performed in the loop; when some 'predicate' tells the element
              should be removed, it is removed and the index is adjusted accordingly.

              The predicate itself looks something like this:

              [code=java]
              private boolen removePredicate (int i) {
              int d= i%10;
              return d+1 == 8 || d == 8 || d-1 == 8;
              }
              [/code]

              This clearly separates the 'business logic' from the 'mechanics'. The predicate
              method takes care of the first, the loop takes care of the mechanics.

              If people are too afraid to build an additional class or method they end up with
              complicated control flow, funny data structures and Swiss army knife methods
              and classes. For some well defined singular functionality never hesitate to
              create a separate method for it. For some well defined entity, never hesitate to
              create a new class for it.

              kind regards,

              Jos

              Comment

              • madhoriya22
                Contributor
                • Jul 2007
                • 251

                #8
                Originally posted by JosAH
                For some well defined singular functionality never hesitate to
                create a separate method for it. For some well defined entity, never hesitate to create a new class for it.

                kind regards,

                Jos
                Hi Jos,
                Nice tip :). I am noting down all ur tips and suggestions for future help.

                Comment

                • sateesht
                  New Member
                  • Apr 2007
                  • 41

                  #9
                  Hi,

                  Below is the Code :

                  <spoonfeeding code removed; it was bad code as well>

                  Cheers,
                  Sateesh.

                  Comment

                  • Nepomuk
                    Recognized Expert Specialist
                    • Aug 2007
                    • 3111

                    #10
                    Originally posted by sateesht
                    Hi,

                    Below is the Code :
                    [CODE=java]
                    <spoonfeeding code removed; it was bad code as well>
                    [/CODE]

                    Cheers,
                    Sateesh.
                    Is there any specific reason, why you have a List of Strings, when you're working with numbers?

                    Greetings,
                    Nepomuk
                    Last edited by JosAH; Sep 6 '07, 12:46 PM. Reason: removed code

                    Comment

                    Working...