【问题标题】:Split ArrayList in multiple ArrayLists where values are same在值相同的多个 ArrayList 中拆分 ArrayList
【发布时间】:2015-03-23 12:47:45
【问题描述】:

嗯,我坚持这一点。

我正在尝试使排序功能这样做。

parentarraylist = [1,1,1,2,3,2,3,3,2,2,1];
*magic*
childarraylist1 = [1,1,1,1];
childarraylist2 = [2,2,2,2];
childarraylist3 = [3,3,3];

神奇的部分是我卡住的地方。

我已经尝试将其放入 for 循环(父级)并检查值。像这样

int i = 0;
int finder = 0;
ArrayList<int> initArray = new ArrayList();
for(int list : parentarraylist){
    if(i == 0){
        finder = list
    }
    if(finder == list){
           initArray.add(list);
           parentarraylist.remove(list);
    }else{
        new ArrayList *value of list* =  new ArrayList(); 
        finder = list;
        *value of list*.add(list);
    }
}

这会导致视图错误,例如 java.util.ConcurrentModificationException 我无法设置 列表的值

我能做些什么来完成这项工作?

【问题讨论】:

  • 进行不同的编码但得到相同的错误但它的抛出不一致。
  • 我总是得到错误....我的代码我试图按日期排序
  • 你能再解释一下输入是什么,期望的输出是什么?
  • 输入为 = parentarraylist = [1,1,1,2,3,2,3,3,2,2,1]; 所需输出为:childarraylist1 = [1,1,1,1]; childarraylist2 = [2,2,2,2]; childarraylist3 = [3,3,3];
  • @Blackbelt 我的代码:stackoverflow.com/questions/29208804/…

标签: java android sorting arraylist


【解决方案1】:

这个小sn-p应该可以帮助您实现目标:

//maps will hold ALL unique integer as it's key
HashMap<Integer, ArrayList<Integer>> maps = new HashMap<Integer, ArrayList<Integer>>();

//your initial array, written inline for clarity
ArrayList<Integer> parentarraylist = new ArrayList<Integer>(Arrays.asList( new Integer[] {1,1,1,2,3,2,3,3,2,2,1}));

//get the iterator so that we won't need another temporary int variable for loop
Iterator<Integer> iterator = parentarraylist.iterator();
while(iterator.hasNext()){
    //next = current integer in our array
    Integer next = iterator.next();

    //check if we have already have current integer or not
    if(!maps.containsKey(next)){
        //we don't have it, initialise an arraylist for this specific integer
        ArrayList<Integer> x = new ArrayList<Integer>();
        x.add(next);

        //put it to our map holder
        maps.put(next, x);
    } else {
        //already have it, add directly
        maps.get(next).add(next);
    }
}

此代码将打印如下内容:

printMap(maps);
//1 = [1, 1, 1, 1]
//2 = [2, 2, 2, 2]
//3 = [3, 3, 3]

printMap() 取自这个答案:Iterate through a HashMap

public static void printMap(Map mp) {
   Iterator it = mp.entrySet().iterator();
   while (it.hasNext()) {
       Map.Entry pair = (Map.Entry)it.next();
       System.out.println(pair.getKey() + " = " + pair.getValue());
       it.remove(); // avoids a ConcurrentModificationException
   }
}

【讨论】:

    【解决方案2】:

    你可以试试这样的:

    public static void main(String[] args) {
            ArrayList<Integer> parent = new ArrayList<Integer>();
            ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
            parent.add(3);
            parent.add(1);
            parent.add(1);
            parent.add(2);
            parent.add(3);
            parent.add(3);
            parent.add(1);
    
            for(int i : parent)
            {
                boolean check = false;
                for(ArrayList<Integer> result : results)
                {
                    if(result.size() > 0)
                    {
                        if(result.get(0) == i)
                        {
                            check = true;
                            result.add(i);
                            break;
                        }
                    }
                }
                if(!check)
                {
                    ArrayList<Integer> temp = new ArrayList<Integer>();
                    temp.add(i);
                    results.add(temp);
                }
            }
    
            for(ArrayList<Integer> i : results)
            {
                for(int j : i)
                {
                    System.out.print("" + j);
                }
                System.out.println();
            }
        }
    

    输出:

    333
    111 
    2
    

    【讨论】:

      【解决方案3】:

      问题是您从正在迭代的数组中删除了一个元素。

      parentArrayList.remove(list);
      

      在此处导致错误。如果删除此行,您的程序将运行。目前我没有看到从 parentArrayList 中删除项目对排序算法的好处,所以只需删除它就可以了。

      【讨论】:

      • 我不知道我是否负担得起(额外的循环)
      猜你喜欢
      • 2014-08-17
      • 1970-01-01
      • 2014-12-29
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多