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

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

问题描述

我试图修改增加的方法,例如,它检查是否一个BinItem的SKU是相同的。如果它是独一无二的,我会添加它。如果有类似的SKU其他人,它会删除BinItem,创建(同一SKU)的一个新的,但总结起来数量。

我想我最好的,但不能让我的头周围......这是我的工作。我试图使一个嵌套的循环,因此经过各一台,检查它们是否是相同的,如果删除它们。它结束了字句。

 公共类斌
{
   私人字符串MYNAME;
   私人的ArrayList< BinItem> myContents;   公共斌(字符串名称)
   {
      MYNAME =名称;
      myContents =新的ArrayList< BinItem>();
   }   公众的ArrayList< BinItem> getContents()
   {
      返回myContents;
   }   公共字符串的getName()
   {
      返回MYNAME;
   }
  //定义捞出totalQuantity方法,并在这里重新定义add方法   公共无效删除(INT B)
   {
      myContents.remove(二);
   }   公众诠释totalQuantity()
   {
      INT X = 0;
      的for(int i = 0; I< myContents.size();我++)
      {
          X + = myContents.get(ⅰ).getQuantity();
      }
      返回X;
   }   公共无效添加(BinItem B)
   {
     的for(int i = 0; I< myContents.size();我++)
     {
         为(中间体X = 0; X&下; myContents.size(); X ++)
         {
             如果(!(myContents.get(ⅰ).getSKU()。等于(myContents.get(x)的.getSKU())))
             myContents.add(二);
             其他
             myContents.remove(二);
         }
     }
   }   公共字符串的toString()
   {
      字符串s =宾+ MYNAME +:\\ n;
      对于(BinItem乙:myContents)
          S + = B +\\ n;
      返回S;
   }
}

这是BinItem类

 公共类BinItem
{
   私人字符串mySKU;
   私人诠释myQuantity;   公共BinItem(字符串SKU,数量INT)
   {
       mySKU = SKU;
       myQuantity =数量;
   }   公共字符串getSKU()
   {
       返回mySKU;
   }   公众诠释getQuantity()
   {
       返回myQuantity;
   }   公共字符串的toString()
   {
       返回SKU+ getSKU()+:+ getQuantity();
   }
}

而这里的主类

 公共静态无效的主要(字串[] args)
{
    彬彬=新宾(A);
    bin.add(新BinItem(1234-0,500));
    bin.add(新BinItem(1234-1,25));
    bin.add(新BinItem(1234-0,243));
    bin.add(新BinItem(1234-2,7720));
    bin.add(新BinItem(1234-0,871));
    的System.out.println(箱);
}

我试图让这个它将返回

 斌答:
SKU 1234-1:25
SKU 1234-2:7720
SKU 1234-0:1614


解决方案

如果你想要做的是检查列表包含BinItem具有一定的SKU,可以覆盖equals方法在BinItem类的一面。

 公众覆盖布尔等于(obj对象){
    如果返回false((OBJ的instanceof斌)!);
    宾B =(彬)目标文件;
    返回b.getSKU()等于(getSKU())。
}

您将要检查如果obj是一个斌对象,否则你会如果传递obj是类型斌没有收到错误消息。
然后在彬类的add方法则需要检查如果列表中包含的目的是通过使用 myContents.contains(NEWOBJECT)结果
如果列表中包含的bin项目已经那么它将返回true。在这一点上,你可以得到已经在列表中的项目的数量,然后添加到它的新对象的数量。有没有需要删除的项目,并创建一个新的,如果你正在改变的是数量。结果
只需添加一个 addQuantity 方法并传递一个int;像这样:

 公共无效addQuantity(INT NUM){
    myQuantity + = NUM​​;
}

要得到那就是alrady你可以使用列表中的对象的indexOf(OBJ)来获得它的索引,然后使用GET检索对象,然后就增加由给定的量的量。结果,
事实上,你甚至不必使用。载有(),因为如果对象没有在列表中,将返回-1,你可以对证,像这样存在:

  INT指数= myContents.indexOf(B);
如果(指数== -1){
    //处理添加项到列表中。
}其他{
    BinItem BI = myContents.get(指数);
    bi.addQuantity(b.getQuantity());
}

希望这有助于你出去。

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.

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

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

And here's the main class

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

I'm trying to make it so that it will return

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

解决方案

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

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

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

Hope this helped you out.

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

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