Results 1 to 13 of 13
- 07-07-2011, 07:07 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
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; }
- 07-07-2011, 07:31 PM #2unSold.add(lots);
- 07-07-2011, 07:51 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
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; } }
- 07-07-2011, 08:05 PM #4
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.
- 07-07-2011, 08:20 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 13
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...
- 07-07-2011, 08:24 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
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;
- 07-07-2011, 08:33 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 13
- 07-07-2011, 08:36 PM #8Is there any way the lot could been copied without the number and the text (No bid)?
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.
- 07-07-2011, 08:37 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
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?
- 07-07-2011, 08:39 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
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?
- 07-07-2011, 08:43 PM #11
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Yes, you have right. I tested it. The return seems to work well. It return the correct number of lot.
Thanks, for the help!
- 07-07-2011, 08:45 PM #12
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.
- 07-07-2011, 08:49 PM #13
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 13
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?
Similar Threads
-
Copy a List<Integer> to an ArrayList
By Nosrettap in forum New To JavaReplies: 3Last Post: 01-16-2011, 07:05 PM -
how to add Arraylist filter for a jsp page showing results from a servlet-Arraylist
By alok_sharma in forum Java ServletReplies: 7Last Post: 11-22-2010, 01:26 PM -
Copy a specified element from one array list to another arraylist
By TaxpayersMoney in forum New To JavaReplies: 1Last Post: 05-20-2010, 10:17 PM -
[SOLVED] ArrayList element to int
By grahamb314 in forum New To JavaReplies: 9Last Post: 11-22-2008, 05:09 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM
Bookmarks