如何修改ArrayList的add [英] How to modify add of ArrayList

查看:23
本文介绍了如何修改ArrayList的add的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改添加"方法,以便检查 BinItem 的 SKU 是否相同.如果它是独一无二的,我会添加它.如果它与其他 SKU 具有相似的 SKU,它将删除 BinItem,创建一个新的(相同 SKU)但总结数量.

I'm trying to modify an "add" method such that it checks if a BinItem's SKU are the same. If it's unique, I will add it. If it has similar SKU as others, it will remove the BinItem, create a new one (of the same SKU) but sum up the Quantities.

我尽了最大的努力,但无法理解……这是我的努力.我尝试制作一个嵌套的 for 循环,以便遍历每个循环,检查它们是否相同并删除它们.它最终删除了所有内容.

I tried my best but couldn't get my head around it... here's my effort. I tried making a nested for loop so it goes through each one, check if they're the same and delete if they are. It ended up deleting everything.

public class Bin
{
   private String myName;
   private ArrayList<BinItem> myContents; 

   public Bin ( String name )  
   {  
      myName = name; 
      myContents = new ArrayList <BinItem>();  
   }

   public ArrayList <BinItem> getContents()  
   {
      return myContents; 
   }

   public String getName() 
   { 
      return myName;  
   }


  // Define the remove and totalQuantity methods and redefine the add method here

   public void remove (int b)
   {
      myContents.remove(b);
   }

   public int totalQuantity()
   {
      int x = 0;
      for (int i = 0; i< myContents.size(); i++)
      {
          x += myContents.get(i).getQuantity();
      }
      return x;
   }

   public void add (BinItem b)
   {
     for (int i = 0; i < myContents.size(); i++)
     {
         for (int x = 0; x < myContents.size(); x++)
         {
             if (!(myContents.get(i).getSKU().equals(myContents.get(x).getSKU())))
             myContents.add(b); 
             else 
             myContents.remove(b);
         }
     } 
   }

   public String toString() 
   {
      String s = "Bin " + myName + ":\n";
      for ( BinItem b : myContents ) 
          s += b + "\n"; 
      return s;
   }
}

这是 BinItem 类

This is the BinItem class

public class BinItem
{
   private String mySKU; 
   private int myQuantity;

   public BinItem ( String sku, int quantity)   
   {   
       mySKU = sku;   
       myQuantity = quantity;   
   }

   public String getSKU()   
   {    
       return mySKU;  
   }

   public int getQuantity()  
   {  
       return myQuantity;  
   }    

   public String toString()  
   {  
       return "SKU " + getSKU() + ": " + getQuantity();  
   }
}

这是主类

public static void main( String[] args )
{
    Bin bin = new Bin( "A" );
    bin.add( new BinItem( "1234-0", 500 ) );
    bin.add( new BinItem( "1234-1", 25 ) );
    bin.add( new BinItem( "1234-0", 243 ) );
    bin.add( new BinItem( "1234-2", 7720 ) );
    bin.add( new BinItem( "1234-0", 871 ) );
    System.out.println( bin );
}

我正在努力让它返回

Bin A:
SKU 1234-1: 25
SKU 1234-2: 7720
SKU 1234-0: 1614

推荐答案

如果您只想检查列表是否包含具有特定 sku 的 BinItem,您可以覆盖 BinItem 类旁边的 equals 方法.

If all you want to do is check if the list contains a BinItem with a certain sku, you can override the equals method in side of your BinItem class.

public override boolean equals(Object obj){
    if(!(obj instanceof Bin)) return false;
    Bin b = (Bin) obj;
    return b.getSKU().equals( getSKU());
}

您需要检查 obj 是否为 Bin 对象,否则如果传递的 obj 不是 Bin 类型,您将收到错误.然后在 Bin 类的 add 方法中,您将使用 myContents.contains( newObject)
检查列表是否包含对象如果列表已经包含 bin 项目,那么它将返回 true.此时,您可以获取列表中已有项目的数量,然后将新对象的数量添加到其中.如果您更改的只是数量,则无需删除项目并创建新项目.
只需添加一个 addQuantity 方法并传入一个 int;像这样:

You will want to check if obj is a Bin object otherwise you will receive an error if the passed obj isn't of type Bin. Then in your add method for the Bin class you would check if the list contains the object by using myContents.contains( newObject)
If the list contains the bin item already then it will return true. at this point you can get the quantity of the item that is already in the list and then add to it the quantity of the new object. There's no need to remove the item and create a new one, if all you are changing is the quantity.
Just add a addQuantity method and pass in an int; Like so:

public void addQuantity(int num){
    myQuantity += num;
}

要获取列表中已经存在的对象,您可以使用 indexOf(obj) 获取它的索引,然后使用 get 检索对象,然后将数量增加给定的数量.
事实上,您甚至不需要使用 .contains(),因为如果该对象不存在于列表中,它将返回 -1,您可以像这样检查:

To get the object that is alrady in the list you can use indexOf(obj) to get the index of it and then retrieve the object using get and then just increment the quantity by the given amount.
In fact you don't even need to use .contains(), since if the object doesn't exist in the list it will return -1 which you can check against like so:

int index = myContents.indexOf( b );
if(index == -1){
    //Handle adding the item to the list.
}else {
    BinItem bi = myContents.get(index);
    bi.addQuantity( b.getQuantity() );
}

希望这对您有所帮助.

这篇关于如何修改ArrayList的add的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆