Java 在值相同的多个ArrayList中拆分ArrayList
嗯,我在这一点上卡住了 我试图使排序功能这是什么Java 在值相同的多个ArrayList中拆分ArrayList,java,android,sorting,arraylist,Java,Android,Sorting,Arraylist,嗯,我在这一点上卡住了 我试图使排序功能这是什么 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> in
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);
}
}
inti=0;
int finder=0;
ArrayList initArray=新的ArrayList();
for(int列表:parentarraylist){
如果(i==0){
查找器=列表
}
if(finder==列表){
initArray.add(列表);
parentarraylist.remove(列表);
}否则{
新建ArrayList*列表的值*=新建ArrayList();
查找器=列表;
*列表*.add(列表)的值;
}
}
这会导致视图错误,如
java.util.ConcurrentModificationException
我不能设置列表的值
我能做些什么来实现这一点呢?问题是您要从正在迭代的数组中删除一个元素。
parentArrayList.remove(list);
在这里导致错误。如果您删除这一行,您的程序将工作。目前,我看不到从parentArrayList中删除该项对排序算法有什么好处,所以只要删除它,就可以了。您可以尝试以下方法:
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();
}
}
这个小片段应该可以帮助您实现目标:
//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()取自以下答案:
做不同的编码,但得到相同的错误,但它抛出不一致。我得到的错误总是。。。。我的代码我正在尝试按日期排序扫描您再次解释什么是输入,什么是所需的输出?输入是=
parentarraylist=[1,1,1,2,3,2,3,3,2,2,1]代码>所需的输出是:childarraylist1=[1,1,1,1];ChildArrayList 2=[2,2,2,2];ChildArrayList 3=[3,3,3]代码>@Blackbelt我的代码:我不知道我是否负担得起(额外的循环)
//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]
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
}
}