开发者

Add element to arraylist and test if the number already exist

开发者 https://www.devze.com 2023-03-21 04:21 出处:网络
I have a method which add element to an arraylist My task is to Modify the addProduct method so that a new product

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.

Since both number and string are in the same word "item" and stored on the same index, I don't know how I can just get the number. I need the number to test to see if the number already exist

An开发者_Go百科y 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);
 }


I would greatly recommend you to go for Set inside the addProduct() method.

From the Javadocs,

SET
A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.

Implement like this,

public static boolean checkDuplicate(ArrayList list) {
 HashSet set = new HashSet();
 for (int i = 0; i < list.size(); i++) {
  boolean val = set.add(list.get(i));
  if (val == false) {
    return val;
  }
 }
 return true;
}


public void addProduct(Product item){
   for (Product p: stock)
      if (p.getId() == item.getId())
         return;
   stock.add(item);
}


I would use a java.util.Set. You would need to implement the equals() and hashcode() methods of the Product class based on the two fields passed into the constructor.


Try using a HashMap with the ID as the Key and the Item as the Value. In an HashMap you cant duplicate Items with the same Key, so your problem is solved at the bottom of your programming. :)


Create a ProductList class that has an ArrayList field and a integer set to keep track of ID's that have been added. When you add an item, check if the set already contains the item's ID. If it doesn't, add the item to the ArrayList. So this basically wraps around an ArrayList quite nicely. Here's how I would do it:

public class ProductList{
...
    private ArrayList<Product> list = new ArrayList<Product>();
    private Set<Integer> ids = new Set<Integer>();
...
    public void addProduct(Product product){
       if(!ids.contains(product.getID())){
            list.add(product);
            ids.add(product.getID());
           }
         }

    public Product removeProduct(Product product){
       if(!list.contains(product)) return null;
       ids.remove(product.getID());
       return list.remove(product);
      }
...
    }

You can then just use

ProductList stock = new ProductList();

and stock.addProduct(Product item); in your other class.

If you think you'll be using your list quite extensively, creating practical constructors to integrate with your data fields will be very useful as well.

This is a very good approach from an abstraction point of view, however it's probably not the most efficient way of doing it.

0

精彩评论

暂无评论...
验证码 换一张
取 消