Results 1 to 13 of 13
  1. #1
    ralf is offline Member
    Join Date
    Jul 2011
    Posts
    98
    Rep Power
    0

    Default ArrayList copy some of the element from one arraylist tnto another arraylist

    I have a method to to find which element in an arraylist that have not any value. The value of the element is null. When they are found they would be copied into a new arraylist.
    I get an output which is very bad, the elements have been multiplied.
    Like this:
    Unsold [[1: chair (No bid), 2: can (No bid), 3: Doll Bid: 450, 4: Termo (No bid)]]
    Unsold [[1: chair (No bid), 2: can (No bid), 3: Doll Bid: 450, 4: Termo (No bid)], [1: chair (No bid), 2: can (No bid), 3: Doll Bid: 450, 4: Termo (No bid)]]
    Unsold [[1: chair (No bid), 2: can (No bid), 3: Doll Bid: 450, 4: Termo (No bid)], [1: chair (No bid), 2: can (No bid), 3: Doll Bid: 450, 4: Termo (No bid)], [1: chair

    The output show also that it has been added more from the first arraylist than it should do.
    I only want things like: Doll, can, Termo and chair to be copied into the new arraylist.

    Some suggestion on what I can do to improve these two problems?

    Java Code:
        public ArrayList getUnsold()
         {
             ArrayList unSold = new ArrayList();
             Iterator it = lots.iterator();
            while(it.hasNext())
            {                        
                  Lot lot = (Lot) it.next(); //number, description, gethighestbid
                     if(lot.getHighestBid() == null)
                     {
                          unSold.add(lots); //It seems that every element are being added multplied times 
                          System.out.println("Unsold " + unSold); //during the iteration
                        }
               }
        return unSold;
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    unSold.add(lots);
    Is this what you want to do?

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

    Default

    Hello,
    I saw that and I have changed the code
    The output is now see below:

    Unsold [1: Hat (No bid), 2: Car (No bid), 3: Wood (No bid), 4: Termo Bid: 450]

    I understand that it copy everything from lot to unsold, even those who have been bid on. that since I add from lot I think, but I thought the if test woudl filter it away



    Java Code:
      public ArrayList getUnsold()
         {
             ArrayList unSold = new ArrayList();
             Iterator it = lots.iterator();
            while(it.hasNext())
            {                        
                  Lot lot = (Lot) it.next(); //number, description, gethighestbid
                     if(lot.getHighestBid() == null)
                     {
                         // unSold.add(lot);
                          //System.out.println("Unsold " + unSold);
                        }
                                  
                        unSold.add(lot);
                        
                    } 
                     System.out.println("Unsold " + unSold);
        return unSold;
        }
     }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Is your code working as you want it now? Is the output correct?

    Your posted code is very hard to read because the ending }s do NOT align beneath the corresponding beginning {s

    It looks like the add() method is outside of the if statement's control.
    Last edited by Norm; 07-07-2011 at 08:07 PM.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    I'm assuming the code has an extra closing curly-brace because it's at the end of the class (or I don't see how it compiles), but whatever, as Norm says, your indentation is screwy - I think you've confused yourself with it, because unSold.add(lot); is actually outside of the null check (which, as posted, does nothing at all) so it gets called for every lot...

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

    Default

    Hello again

    The output is now:


    Unsold [1: Car (No bid), 3: Cup (No bid)]
    Is the code easier to read now? I added three lot and bidded on one of them, and now it return and print out just the two without ane bid on, so it seems correct.
    Is there any way the lot could been copied without the number and the text (No bid)?


    Java Code:
        public ArrayList getUnsold()
         {
             ArrayList unSold = new ArrayList();
             Iterator it = lots.iterator();
              while(it.hasNext())
               {                        
                  Lot lot = (Lot) it.next(); //number, description, gethighestbid
                     if(lot.getHighestBid() == null)
                       {
                         unSold.add(lot);
                       }
         // unSold.add(lot);
               } 
     System.out.println("Unsold " + unSold);
        return unSold;

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by ralf View Post
    Is there any way the lot could been copied without the number and the text (No bid)?
    I can't parse that - could you rephrase it more clearly?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Is there any way the lot could been copied without the number and the text (No bid)?
    I think you are confusing what is printed when you print the unSold object: System.out.println("Unsold " + unSold);

    with what the unSold list contains. The toString() method in the Lot class creates the output that is displayed when you print it.
    For an easy test to see that, go to the Lot class and make a small change to the text that is returned by the toString() method and run your program again. You should see the change that you make on the print out.

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

    Default

    Hello,

    Yes, I mean if you see the output: Unsold [1: Car (No bid), 3: Cup (No bid)]

    Everything from the first arraylist has been copied into the new one. Like 1: (No bid)
    I was wondering if it could just copy the items like: Car, Cup?

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

    Default

    Hello,

    Yes, I mean if you see the output: Unsold [1: Car (No bid), 3: Cup (No bid)]

    Everything from the first arraylist has been copied into the new one. Like 1: (No bid)
    I was wondering if it could just copy the items like: Car, Cup?

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

    Default

    Yes, you have right. I tested it. The return seems to work well. It return the correct number of lot.

    Thanks, for the help!

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    You are copying the reference to a Lot object.
    If you only what the contents of one of the variables in a Lot object, you need to change your code to copy just that variable instead of the whole Lot object.

  13. #13
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by ralf View Post
    Yes, I mean if you see the output: Unsold [1: Car (No bid), 3: Cup (No bid)]

    Everything from the first arraylist has been copied into the new one.
    No, only the unsold items have been copied. That's what you wanted, isn't it?

    I was wondering if it could just copy the items like: Car, Cup?
    If you just want to copy the names of the Lots, then just copy the names into the arraylist, but why? it doesn't save you anything - both Lots and Strings will be stored as references; you might just as well store the Lots.

Similar Threads

  1. Copy a List<Integer> to an ArrayList
    By Nosrettap in forum New To Java
    Replies: 3
    Last Post: 01-16-2011, 07:05 PM
  2. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  3. Replies: 1
    Last Post: 05-20-2010, 10:17 PM
  4. [SOLVED] ArrayList element to int
    By grahamb314 in forum New To Java
    Replies: 9
    Last Post: 11-22-2008, 05:09 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

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