Results 1 to 18 of 18
- 07-17-2011, 11:14 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Method to add element to arraylist avoid adding the same id number twice
Hello,
I have a method which add element to an arraylist
My task is to Modify the addProduct method so that a new product
cannot be added to the product list with the same ID
as an existing one.
I have tryed to use an if test to see if there are some id number allready in the list, but it wont work.
Any suggestion on how I should do this?
The way I add to the arraylist is like this below:
(new Product(132, "Clock Radio"))
public void addProduct(Product item)
{
stock.add(item);
}
- 07-17-2011, 11:19 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What do you mean by "it won't work"?
You can loop through the array list and see if any items have matching ids
- 07-17-2011, 11:30 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello,
I have tried this
Any idea what I'm doing wrong?
Java Code:Iterator it = stock.iterator(); while(it.hasNext()) { StockManager stockmanager = (StockManager) it.next(); if(Product.getID() != item) { System.out.print("This id exist already " + item); } else{ stock.add(item); } }
- 07-18-2011, 12:28 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What if item 1 does contain the matching id? Does it stop checking and realize that it shouldn't be added? Or does it add the item? What if the first item does not match, but the second does? Should it add the item? Does it?
There is a minor bug that I hope these questions help you understand.
- 07-18-2011, 12:59 AM #5
Where in the code you posted do you use the StockManager object?
If you only want unique objects in your collection then you should use a Set which does not allow duplicates.
- 07-18-2011, 01:13 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Agreed with junky, amswt will automatically remove duplicates for you. However; you will have to override equals and hashcode, make sure you override equals and not overload it. It is fairly trivial to implement if with ArrayList of that's what you are required to do, and your mistake is quite trivial(it's just a matter of noticing it).
- 07-18-2011, 02:13 AM #7
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
Hello,
I have tried to change the code to this, but stil it don't work.
Any suggestion?
Java Code:Iterator it = stock.iterator(); while(it.hasNext()) { Product product = (Product) it.next(); //StockManager stockmanager = (StockManager) it.next(); if(product == item) { System.out.print("This id exist already " + item); } else{ stock.add(item); } }
- 07-18-2011, 02:36 AM #8
- 07-18-2011, 02:54 AM #9
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
With that code I have now I'm not able to add anything to the list at all. I'am trying to check if an id number exist already in the list.
I don't know if it is better to use a for loop or an iterator.Java Code:public void addProduct(Product item) { Iterator it = stock.iterator(); while(it.hasNext()) { Product product = (Product) it.next(); //StockManager stockmanager = (StockManager) it.next(); if( product.getID() == item) { System.out.print("This id exist already " + item); } else{ stock.add(item); } }
if I try to compile then I will get an error messages that product and int is incomparable types. I don't know what I should use instead of product.getID() since the getID is returning the id from the listLast edited by ralf; 07-18-2011 at 02:57 AM.
- 07-18-2011, 03:05 AM #10
I said to include the full and exact error message and not to paraphrase. Do you want help or do you just want to continue this pointless exercise of providing little to no information and expect us to guess?
What is stock?
What does it contain?
Look at your if statement. If getId returns an int you are then trying to compare it to a Product object. Does if(7 == Chair) make sense to you?
- 07-18-2011, 09:37 AM #11
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
stock is name of the arraylist, and it contain id number and name of the product.
Yes, I know you can't do 7 == chair.
as you can see I add the product id and the name into the parameter, such as the word item contain both an int and a string value.
I don't know how I can get the id number from the word item so I can use that number to compare it to other number already in the list.
The add method and the arraylist stock are in a class named StockManager and the method getID() is in the class product
Java Code:new Product(132, "Clock Radio")) public void addProduct(Product item) { stock.add(item); }
- 07-18-2011, 09:47 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
I'd implement a Comparator<T> that checks on those IDs and use a Set of some sort to store the unique (according to that Comparator<T> implementation) elements.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-18-2011, 02:12 PM #13
Member
- Join Date
- Jul 2011
- Posts
- 98
- Rep Power
- 0
I don't know how I could use Comparator<T>.
In the word item there are a number and string. How can get just the number out from that word? The word item is in the parameter.
public void addProduct(Product item)
I need to get the number so I can use it to compare to other number stored in the list, to see if they are ==
- 07-19-2011, 12:29 AM #14
- 07-19-2011, 12:31 AM #15
-
Junky, check out his previous thread: Method Call. Norm has the patience of a saint.
- 07-19-2011, 12:50 AM #17
No, the way to say it is "the stubbornness of a mule".
I'm currently living in Missouri.
- 07-19-2011, 12:53 AM #18
Similar Threads
-
ArrayList copy some of the element from one arraylist tnto another arraylist
By ralf in forum New To JavaReplies: 12Last Post: 07-07-2011, 08:49 PM -
[SOLVED] ArrayList element to int
By grahamb314 in forum New To JavaReplies: 9Last Post: 11-22-2008, 05:09 PM -
adding list to an array element
By Preethi in forum New To JavaReplies: 5Last Post: 09-25-2008, 04:23 AM -
Adding a double element to a vector
By peachyco in forum New To JavaReplies: 5Last Post: 11-25-2007, 06:07 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks