如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
让我们假设您想要将列表分割为多个块的类作为库类。
所以让我们说这个类被称为“shared”,in应该是final,以确保它不会被扩展。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class Shared {
List<Integer> input;
int portion;
public Shared(int portion, Integer... input) {
this.setPortion(portion);
this.setInput(input);
}
public List<List<Integer>> listToChunks() {
List<List<Integer>> result = new ArrayList<List<Integer>>();
int size = this.size();
int startAt = 0;
int endAt = this.portion;
while (endAt <= size) {
result.add(this.input.subList(startAt, endAt));
startAt = endAt;
endAt = (size - endAt < this.portion && size - endAt > 0) ? (this.size()) : (endAt + this.portion);
}
return result;
}
public int size() {
return this.input.size();
}
public void setInput(Integer... input) {
if (input != null && input.length > 0)
this.input = Arrays.asList(input);
else
System.out.println("Error 001 : please enter a valid array of integers.");
}
public void setPortion(int portion) {
if (portion > 0)
this.portion = portion;
else
System.out.println("Error 002 : please enter a valid positive number.");
}
}
接下来,让我们尝试从另一个持有公共静态void main(String…args)
public class exercise {
public static void main(String[] args) {
Integer[] numbers = {1, 2, 3, 4, 5, 6, 7};
int portion = 2;
Shared share = new Shared(portion, numbers);
System.out.println(share.listToChunks());
}
}
现在,如果输入一个整数数组[1,2,3,4,5,6,7],分区为2。 结果将是[[1,2],[3,4],[5,6],[7]]
其他回答
使用StreamEx库,您可以使用StreamEx。ofSubLists(List<T> source, int length)方法:
返回一个新的StreamEx,它由给定源列表的不重叠子列表组成,具有指定的长度(最后一个子列表可能更短)。
// Assuming you don't actually care that the lists are of type ArrayList
List<List<Integer>> sublists = StreamEx.ofSubLists(result, 10).toList();
// If you actually want them to be of type ArrayList, per your question
List<List<Integer>> sublists = StreamEx.ofSubLists(result, 10).toCollection(ArrayList::new);
只是要明确一点,这还需要更多的测试…
public class Splitter {
public static <T> List<List<T>> splitList(List<T> listTobeSplit, int size) {
List<List<T>> sublists= new LinkedList<>();
if(listTobeSplit.size()>size) {
int counter=0;
boolean lastListadded=false;
List<T> subList=new LinkedList<>();
for(T t: listTobeSplit) {
if (counter==0) {
subList =new LinkedList<>();
subList.add(t);
counter++;
lastListadded=false;
}
else if(counter>0 && counter<size-1) {
subList.add(t);
counter++;
}
else {
lastListadded=true;
subList.add(t);
sublists.add(subList);
counter=0;
}
}
if(lastListadded==false)
sublists.add(subList);
}
else {
sublists.add(listTobeSplit);
}
log.debug("sublists: "+sublists);
return sublists;
}
}
Java8流,一个表达式,没有其他库:
List<String> input = ...
int partitionSize = ...
Collection<List<String>> partitionedList = IntStream.range(0, input.size())
.boxed()
.collect(Collectors.groupingBy(partition -> (partition / partitionSize), Collectors.mapping(elementIndex -> input.get(elementIndex), Collectors.toList())))
.values();
测试:
List<String> input = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h" ,"i");
partitionSize = 1 - > [[a], [b], [c], [d], [e], [f], [g], [h],[我]] partitionSize = 2 - > [[a, b], c, d, e, f, g, h,[我]] partitionSize = 3 - > [a, b, c, d, e, f, g, h,我]] partitionSize = 7 - > [[a, b, c, d, e, f, g], [h,我]] partitionSize = 100 -> [[a, b, c, d, e, f, g, h, i]]
你可以使用subList(int fromIndex, int toIndex)来获取原始列表的一部分。
来自API:
返回该列表中指定的fromIndex(包含)和toIndex(不包含)之间部分的视图。(如果fromIndex和toIndex相等,返回的列表为空。)返回的列表由该列表支持,因此返回列表中的非结构性更改会反映在该列表中,反之亦然。返回的列表支持该列表支持的所有可选列表操作。
例子:
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]"
如果您需要这些切碎的列表不是一个视图,那么只需从subblist创建一个新的List。下面是一个把这些东西放在一起的例子:
// 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!)
polygenelubricants提供的答案将基于给定数组的大小。我正在寻找将数组分割成给定数量的部分的代码。以下是我对代码所做的修改:
public static <T>List<List<T>> chopIntoParts( final List<T> ls, final int iParts )
{
final List<List<T>> lsParts = new ArrayList<List<T>>();
final int iChunkSize = ls.size() / iParts;
int iLeftOver = ls.size() % iParts;
int iTake = iChunkSize;
for( int i = 0, iT = ls.size(); i < iT; i += iTake )
{
if( iLeftOver > 0 )
{
iLeftOver--;
iTake = iChunkSize + 1;
}
else
{
iTake = iChunkSize;
}
lsParts.add( new ArrayList<T>( ls.subList( i, Math.min( iT, i + iTake ) ) ) );
}
return lsParts;
}
希望它能帮助到别人。