Page 2 of 2 FirstFirst 12
Results 21 to 38 of 38
  1. #21
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello,
    after further testing of the method it turn out that it don't work as expected.
    I add three element to the list: 1 A, 2 B and 3 C.
    The method is only abel to remove element which is on index 0 and not remove the number I want it to remove.
    Any idea what I should do?

    Java Code:
      public Lot removeLot(int number)
      {
            if(number < 1)
           {
               System.out.println("This number does not exist " + number);
            }
           else{
               Iterator it = lots.iterator();
              while(it.hasNext())
                {                        
                 Lot lot = (Lot) it.next(); //number, description, gethighestbid
                     if(number == lot.getNumber())
                       {
                          lots.remove(lot);
                       }
             return lot;
                }
             }
    return null;
     }

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    not remove the number I want it to remove
    Can you describe the problem? Does the code get an exception?
    Does code execute without an exception but not do what you want it to do?

  3. #23
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    For example if I try to remove number 3 from the list. The code execute without an exception, nothing happen. It don't remove number 3. But in the debugger I can see that number 1 on index 0 being pointed at. It just point at number 1 and then finish to execute without doing anything.
    There is no problem to remove number 1 on index 0.

    I want it to remove the number I insert into the parameter, but the method don't do that

  4. #24
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    I want it to remove the number I insert into the parameter, but the method don't do that
    Please explain?

    Please write a small test program that demonstrates your problem. See post #20 for a sample.

  5. #25
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello,

    I tested out the code you wrote on post 20, I got the same error as you did.

    I have now tryed to use listiterator instead of iterator, and now I have the opposite problem from what I had befor. With iterator I could only remove elements from index 0, although I inserted number 3 to be removed.
    Now with listiterator I can only remove the last element in the index.
    could anyone tell me what I am doing wrong?
    Here is the listiterator code:
    Java Code:
     public Lot removeLot(int number)
      {
        if(number < 1)
            {
               System.out.println("This number does not exist " + number);
            }
            else{
                    
                ListIterator iter = lots.listIterator(lots.size());
              //  System.out.println(lots);
                     while (iter.hasPrevious()) 
                     {
                         Lot lot = (Lot) iter.previous(); //number, description, gethighestbid
                      //   System.out.println(lots);
                            if(number == lot.getNumber())
                          {
                          lots.remove(lot);
                          }
                         //System.out.println(iter.previous());
                       return lot;
                        }
                      }
               return null;
       }

  6. #26
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    what I am doing wrong
    Please make a small complete program that compiles and executes and demonstrates the problem.
    Create a list, add to the list, print the list, remove from the list and print the list after the remove is done.
    Last edited by Norm; 07-11-2011 at 07:36 PM.

  7. #27
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    I found this example and have tryed to edit it to my task, but have now idea on how to do it.
    This example below is ablel to add, print and remove.
    Any idea on how it can be editet to my task?

    Java Code:
    public class Simple {
    
      public static void main(String args[]) {
        ArrayList<String> list = new ArrayList<String>();
    
        list.add("Bart");
        list.add("Lisa");
        list.add("Marge");
        list.add("Barney");
        list.add("Homer");
        list.add("Maggie");
    
        for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
          String s = iter.next();
          if (s.equals("Barney")) {
            iter.remove();
          }
          else {
            System.out.println(s);
          }
        }
    
        for(String s: list)
        {
          System.out.println(s);
        }
      }
    }

  8. #28
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    Does the posted code work? Did it do what you wanted?

  9. #29
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Yes, the posted code work. It removes "Barney".
    I have tried to adapt it to my task, but I dont know how it should be done.
    any suggestions?

  10. #30
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    You need to put the working part of the demo code in the method and replace the hardcoding of the String being tested for with the parameter being passed to the method.

  11. #31
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello,

    I have now tryeda lots of different things and I can't get it to work as expected.
    Could it be better to use listiterator? Anyone know how I can use listiterator to solve this task?
    I have tryed that one to, but it stil don't work

  12. #32
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello,

    Could anyone help me with this method? I am stuck

  13. #33
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    Do You have a working example of the code you need to merge into your method?
    Can you explain what problems you are having merging the code?

  14. #34
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello, I hope you can solve this Norm

    I am trying to use listiterator to iterat reverse. When I use print, the correct number being printed out, but when I use the remove I get this error code:

    java.util.ConcurrentModificationException
    at java.util.AbstractList$Itr.checkForComodification( AbstractList.java:372)
    at java.util.AbstractList$ListItr.previous(AbstractLi st.java:386)
    at Auction.removeLot(Auction.java:156)


    Java Code:
     public Lot removeLot(int number)
      {
       if(number <=0)
           {
               System.out.println("This number does not exist " + number);
            }
           else{
                
    
               ListIterator it = lots.listIterator(lots.size());
               while(it.hasPrevious()) 
               {
               Lot lot = (Lot) it.previous();
               if(number == lot.getNumber())
               {
                    //System.out.println(lot); //When I use the print, it print out the correct number, but when I use the remove I get an error code
                    lots.remove(lot);               
                }
          }
      }
    return null;
    }

  15. #35
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    ConcurrentModificationException
    Have you read the API doc for this class? It explains why it is happening.

    Change your loop to only find the index (and save it) of the element to be removed from the list and exit the loop.
    Then after the loop, if an element has been found, now remove it from the list using the saved index.

  16. #36
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    Hello

    Yes, you have right, it work now, but when I add a return, the iterator stop to iterate, Whe should I put the return?
    Java Code:
               ListIterator it = lots.listIterator(lots.size());
               while(it.hasPrevious()) 
               {
               Lot lot = (Lot) it.previous();
               if(number == lot.getNumber())
               {
                  it.remove();   //This remove the correct number, but when I add a return then the iterator don't iterate, it stop.            
                }          
        
        }
        return lot;
      }
    return null;
    }
    
    }

  17. #37
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,891
    Rep Power
    25

    Default

    You should use return when the method is done with its job and you have the value you want to return.

  18. #38
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default

    This method is working perfect now!
    Thanks, for the help!


    Java Code:
     public Lot removeLot(int number)
      {
       if(number <=0)
           {
               System.out.println("This number does not exist " + number);
            }
           else{
               
               ListIterator it = lots.listIterator(lots.size());
               while(it.hasPrevious()) 
               {
               Lot lot = (Lot) it.previous();
                if(number == lot.getNumber())
               {
                  it.remove();               
                  return lot;  
                }           
          }
        }
    return null;
      }

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. ArrayList and remove()
    By Pragz in forum New To Java
    Replies: 1
    Last Post: 04-21-2011, 02:42 AM
  3. remove element contents
    By oliveira in forum XML
    Replies: 5
    Last Post: 12-07-2010, 09:44 AM
  4. Replies: 1
    Last Post: 05-20-2010, 11:17 PM
  5. [SOLVED] ArrayList element to int
    By grahamb314 in forum New To Java
    Replies: 9
    Last Post: 11-22-2008, 06:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •