【问题标题】:How would I add values based on one parameter in Custom Array List?如何根据自定义数组列表中的一个参数添加值?
【发布时间】:2017-02-20 07:56:42
【问题描述】:

如何在自定义数组列表中添加包含变量 itemcode (String) 、 case(int) 、 packs (int) 、 pcs (int) 的 bean 类的值 我在数组列表中保存了多个值

 {code1 , 3,4,5}  
    {code2 , 3,5,5}  
    {code3 , 3,2,5}  
    {code3 , 1,4,2}  
    {code1 , 3,3,5}  
    {code3 , 3,4,5}  
    {code2 , 3,8,5}  
    {code2 , 3,4,6}

在这种情况下,我需要结果为

{code1 ,6,7,10 }
{code2 ,9,17,16 }
{code3 ,7,10,12}  

【问题讨论】:

  • 请显示一些实际代码。并准确地解释你卡在哪里。
  • 我正在查看这个示例 stackoverflow.com/questions/25699131/…
  • @Yatin:是什么阻止您使用您链接到的问题中给出的答案?

标签: java android arrays arraylist


【解决方案1】:

我不确切知道您的 bean 的外观,但使用 HashMap 和循环,您可以根据需要“分组和求和”。 它可能看起来像这样:

public static List<BeanClass> sumPerItemCode
    (List<BeanClass> list) {

    Map<String, BeanClass> map = new HashMap<>();

    for (BeanClass p : list) {
        String code = p.getItemCode();
        BeanClass aggregatedItem = map.get(code);
        if (aggregatedItem == null) {
            aggregatedItem = new BeanClass(code, 0,0,0);
            map.put(code, aggregatedItem);
        }

        aggregatedItem.setCases(aggregatedItem.getCases() + p.getCases());
        aggregatedItem.setPacks(aggregatedItem.getPacks() + p.getPacks());
        aggregatedItem.setPcs(aggregatedItem.getPcs() + p.getPcs());
    }
    return new ArrayList<BeanClass>(map.values());
}

【讨论】:

    【解决方案2】:

    我建议你使用 Map 而不是 List。

    如果键相同,则映射替换现有值的值。

    您要做的是首先检查是否有可用的键获取该值并进行添加,然后再次使用相同的键将其存储在 Map 中。

    `

    【讨论】:

      【解决方案3】:

      试试看 -

      ArrayList<String> list = new ArrayList<String>();
      list.add("code1");               //itemcode
      list.add(String.valueOf(100));   //cases
      list.add(String.valueOf(150));   //packs
      list.add(String.valueOf(200));   //pcs
      

      并从 arrayList 中获取数据 -

      String itemcode  = list.get(0);              //code1
      int cases = Integer.parseInt(list.get(1));   //100
      int packs = Integer.parseInt(list.get(2));   //150
      int pcs = Integer.parseInt(list.get(3));     //200
      

      但对于这种具有相同机制的场景,我总是建议使用Map 而不是ArrayList。因为 Map 可以将数据存储为键值对。

      【讨论】:

        【解决方案4】:

        像这样创建一个 Bean 类
        public class BeanClass{
        public String code;
        public int integerOne;
        public int integerTwo;
        public int integerThree;
        @ 987654326@
        this.code=code;
        this.integerOne=integerOne;
        this.integerTwo=integerTwo;
        this.integerThree=integerThree
        }
        }
        在主类中
        ArrayList&lt;BeanClass&gt;beanclassArrayList=new ArrayList&lt;&gt;();
        for(int i=0;i&lt;YourArrray[].length();i++){
        BeanClass beanClass=new BeanClass(codeArray[i],integerOneArray[i],integerTwoArray[i],integerThreeArray[i]);
        beanclassArrayList.add(beenClass);
        }

        【讨论】:

        • 您可以稍微格式化一下您的答案吗?它在当前状态下完全不可读。
        • **检查一下**@Henrik
        猜你喜欢
        • 2014-09-02
        • 2021-01-02
        • 2021-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多