如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
如何将一个数组列表(size=1000)拆分为多个相同大小(=10)的数组列表?
ArrayList<Integer> results;
当前回答
如果你不想导入apache Commons库,试试下面这段简单的代码:
final static int MAX_ELEMENT = 20;
public static void main(final String[] args) {
final List<String> list = new ArrayList<String>();
for (int i = 1; i <= 161; i++) {
list.add(String.valueOf(i));
System.out.print("," + String.valueOf(i));
}
System.out.println("");
System.out.println("### >>> ");
final List<List<String>> result = splitList(list, MAX_ELEMENT);
for (final List<String> entry : result) {
System.out.println("------------------------");
for (final String elm : entry) {
System.out.println(elm);
}
System.out.println("------------------------");
}
}
private static List<List<String>> splitList(final List<String> list, final int maxElement) {
final List<List<String>> result = new ArrayList<List<String>>();
final int div = list.size() / maxElement;
System.out.println(div);
for (int i = 0; i <= div; i++) {
final int startIndex = i * maxElement;
if (startIndex >= list.size()) {
return result;
}
final int endIndex = (i + 1) * maxElement;
if (endIndex < list.size()) {
result.add(list.subList(startIndex, endIndex));
} else {
result.add(list.subList(startIndex, list.size()));
}
}
return result;
}
其他回答
只是要明确一点,这还需要更多的测试…
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;
}
}
List<List<Integer>> allChunkLists = new ArrayList<List<Integer>>();
List<Integer> chunkList = null;
int fromIndex = 0;
int toIndex = CHUNK_SIZE;
while (fromIndex < origList.size()) {
chunkList = origList.subList(fromIndex, (toIndex > origList.size() ? origList.size() : toIndex));
allChunkLists.add(chunkList);
fromIndex = toIndex;
toIndex += CHUNK_SIZE;
}
没有库,只有Java的subList()。toIndex需要适当地有界,以避免在subList()中出现越界错误。
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;
}
希望它能帮助到别人。
使用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);
让我们假设您想要将列表分割为多个块的类作为库类。
所以让我们说这个类被称为“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]]