Java的:我怎么能一分为多个小的ArrayList一个ArrayList? [英] Java: how can I split an ArrayList in multiple small ArrayLists?
问题描述
我怎样才能在相同的尺寸(= 10)?
多的ArrayList分裂一个ArrayList(大小= 1000) 的ArrayList<整数GT;结果;
您可以使用<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/List.html#subList%28int,%20int%29\"><$c$c>subList(int fromIndex,则INT元素范围) 来获得原始列表的一部分的看法。
从API:
返回的这个名单的部分视图指定的
的fromIndex
,包容和元素范围
,排斥。 (如的fromIndex
和元素范围
相等,则返回的列表为空。)返回的列表受此列表的支持,在返回的列表中,这样的非结构性更改将反映在此列表中,反之亦然。返回的列表支持所有通过此列表支持的可选列表操作。
块引用>例如:
列表&LT;整数GT;数=新的ArrayList&LT;整数GT;(
Arrays.asList(5,3,1,2,9,5,0,7)
);
清单&LT;整数GT;头= numbers.subList(0,4);
清单&LT;整数GT;尾= numbers.subList(4,8);
的System.out.println(头); //输出[5,3,1,2]
的System.out.println(尾); //输出[9,5,0,7]
Collections.sort(头);
的System.out.println(数字); //输出[1,2,3,5,9,5,0,7]
tail.add(-1);
的System.out.println(数字); //输出[1,2,3,5,9日,5,0,7,-1]如果你需要这些切碎的名单中而不是视图,然后只需创建一个新的
列表
从子列表
。这里是把其中的一些东西放在一起的例子://扒列表成非视图长度L子列表
静态&LT; T&GT;清单&LT;名单,LT; T&GT;&GT;切碎(列表&LT; T&GT;清单最终诠释L){
清单&LT;名单,LT; T&GT;&GT;部分=新的ArrayList&LT;名单,LT; T&GT;&GT;();
最终诠释N =则为list.size();
的for(int i = 0; I&LT; N,I + = L){
parts.add(新的ArrayList&LT; T&GT;(
list.subList(ⅰ,Math.min(N,I + L)))
);
}
返回部分;
}
清单&LT;整数GT;数= Collections.unmodifiableList(
Arrays.asList(5,3,1,2,9,5,0,7)
);
清单&LT;名单,LT;整数GT;&GT;份=切碎(数字,3);
的System.out.println(件); //输出[[5,3,1],[2,9,5],[0,7]]
parts.get(0)。新增(-1);
的System.out.println(件); //输出[[5,3,-1],[2,9,5],[0,7]]
的System.out.println(数字); //输出[5,3,1,2,9,5,0,7](未修饰!)How can I split an ArrayList (size=1000) in multiple ArrayLists of the same size (=10) ?
ArrayList<Integer> results;
解决方案You can use
subList(int fromIndex, int toIndex)
to get a view of a portion of the original list.From the API:
Returns a view of the portion of this list between the specified
fromIndex
, inclusive, andtoIndex
, exclusive. (IffromIndex
andtoIndex
are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.Example:
List<Integer> numbers = new ArrayList<Integer>( Arrays.asList(5,3,1,2,9,5,0,7) ); List<Integer> head = numbers.subList(0, 4); List<Integer> tail = numbers.subList(4, 8); System.out.println(head); // prints "[5, 3, 1, 2]" System.out.println(tail); // prints "[9, 5, 0, 7]" Collections.sort(head); System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7]" tail.add(-1); System.out.println(numbers); // prints "[1, 2, 3, 5, 9, 5, 0, 7, -1]"
If you need these chopped lists to be NOT a view, then simply create a new
List
from thesubList
. Here's an example of putting a few of these things together:// chops a list into non-view sublists of length L static <T> List<List<T>> chopped(List<T> list, final int L) { List<List<T>> parts = new ArrayList<List<T>>(); final int N = list.size(); for (int i = 0; i < N; i += L) { parts.add(new ArrayList<T>( list.subList(i, Math.min(N, i + L))) ); } return parts; } List<Integer> numbers = Collections.unmodifiableList( Arrays.asList(5,3,1,2,9,5,0,7) ); List<List<Integer>> parts = chopped(numbers, 3); System.out.println(parts); // prints "[[5, 3, 1], [2, 9, 5], [0, 7]]" parts.get(0).add(-1); System.out.println(parts); // prints "[[5, 3, 1, -1], [2, 9, 5], [0, 7]]" System.out.println(numbers); // prints "[5, 3, 1, 2, 9, 5, 0, 7]" (unmodified!)
这篇关于Java的:我怎么能一分为多个小的ArrayList一个ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!