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

    Default 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);
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What do you mean by "it won't work"?

    You can loop through the array list and see if any items have matching ids

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

    Default

    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);
                }
            }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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).

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

    Default

    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);
                }
            }

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by ralf View Post
    but stil it don't work.
    Are you serious? Do you really think that anyone has a friggin clue as to what your problem is without miore information. If you get error messages then post them, full and exact not paraphrased.

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

    Default

    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.


    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);
                   }
            }
    I don't know if it is better to use a for loop or an iterator.

    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 list
    Last edited by ralf; 07-18-2011 at 02:57 AM.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by ralf View Post
    if I try to compile then I will get an error messages that product and int is incomparable types.
    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?

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

    Default

    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);
    }

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    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 ==

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by ralf View Post
    I know you can't do 7 == chair.
    So why do you think that (10 == Product) is ok then?

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by ralf View Post
    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 ==
    All you need to do is take a few moments to think.
    Java Code:
    if( product.getID() == item)
    What is product?
    What is item?
    What is the very simple change you have to make to get that if statement to work?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Junky, check out his previous thread: Method Call. Norm has the patience of a saint.

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

    Default

    No, the way to say it is "the stubbornness of a mule".
    I'm currently living in Missouri.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    check out his previous thread
    It makes you wonder when it takes 3 pages to resolve an issue.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. [SOLVED] ArrayList element to int
    By grahamb314 in forum New To Java
    Replies: 9
    Last Post: 11-22-2008, 05:09 PM
  3. adding list to an array element
    By Preethi in forum New To Java
    Replies: 5
    Last Post: 09-25-2008, 04:23 AM
  4. Adding a double element to a vector
    By peachyco in forum New To Java
    Replies: 5
    Last Post: 11-25-2007, 06:07 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
  •