我想将一个arraylist子列表并存储在小型arraylists中。
我正在做如下但无法在小数组中输入确切的值。请查看我的代码并建议其他方式或告诉我我做错了什么
ArrayList<ques_details> queslist = new ArrayList<ques_details>();
ArrayList[] resgrp = new ArrayList[queslist.size() / 2];
Log.v("length", resgrp.length + "");
for (int i = 0; i < resgrp.length ; i++) {
resgrp[i] = new ArrayList();
Log.v("initialised ", i + "");
}
for (int i = 0; i <= queslist.size()-1 ; i++) {
resgrp[i].add(queslist.get(i));
Log.v("final ", resgrp[i].size() + "");
}
编辑:
public void subListArray(int start, int end) {
// Log.v("queslist size", queslist.size() + "");
int m = queslist.size();
// Log.v("m", m + "");
ArrayList[] resgrp = new ArrayList[m / 2];
// Log.v("length", resgrp.length + "");
int n = resgrp.length;
// Log.v("n", n + "");
int o = m / n;
// Log.v("o", o + "");
for (int i = 0; i < n; i++) {
resgrp[i] = new ArrayList<String>();
Log.v("initialised ", i + "");
}
ArrayList<String> TempList = new ArrayList<String>();
for (int i = start; i <= end - 1; i++) {
int q = 0 ;
String temp = queslist.get(i).Ques;
resgrp[o].add(q, temp);
// resgrp[i].add(queslist.get(i));
Log.v("final ", queslist.get(i).Ques + "");
TempList = resgrp[o];
q++;
adapter = new ArrayAdapter(E_Learning_AppActivity.this,
R.layout.list_item, R.id.text, TempList);
}
}
答案 0 :(得分:6)
我建议你使用:
Lists.partition(List, int)
它会将List
分成指定List
尺寸的int
:
List<Stuff> l = new ArrayList<Stuff>();
// [...] populate l with Stuff here [...]
// partitioning:
List<List<Stuff>> ll = Lists.partition(l, 5);
// you now have a list containing sub-lists of at most 5 elements
备注:强>
List.subList
(见下文)。List.subList(int, int)
package com.stackoverflow.haylem.sublists;
import java.util.ArrayList;
import java.util.List;
public class SubLists {
public static <T> List<List<T>> partition(List<T> l, final int nPartitions) {
final List<List<T>> partitions = new ArrayList<List<T>>(nPartitions);
final int nElements = l.size() / nPartitions; // number of elements per full partition
final int nRest = l.size() % nElements; // size of the last partition (if any)
for (int i = 0; i < nPartitions; i++) { // create our nPartitions partitions
partitions.add(l.subList( // one subList per partition
i * nElements,
i * nElements + nElements
));
}
if (nRest > 0) { // remainder sublist
partitions.add(l.subList(
nPartitions * nElements,
(nPartitions * nElements) + nRest));
}
return (partitions);
}
/**
* Generates a dummy list for testing
*/
public static List<String> generateStringList(final int size) {
final List<String> data = new ArrayList<String>(size);
for (int i = 0; i < 129; i++) {
data.add("String " + i);
}
return (data);
}
/**
* Prints out all the sublists to visualize partitioning
*/
public static <T> void printSubLists(final List<List<T>> sLists) {
for (int i = 0; i < sLists.size(); i++) { // iterates over all sublists
System.out.println("partition " + i);
for (final T element : sLists.get(i)) { // prints out current sublist
System.out.println(" " + element); // prints out current element
}
}
}
public static void test() {
final List<String> data = generateStringList(129);
// splits l in five partitions and
// prints out 5 partitions of 25 elements and 1 of 4
printSubLists(partition(data, 5));
// splits l in partitions of 4 or less elements and
// prints out 32 partitions of 4 elements and 1 of 1
printSubLists(partition(data, data.size() / 4));
}
}
备注:强>
答案 1 :(得分:4)
您正在创建queslist.size() / 2
列表来存储“子列表”
ArrayList[] resgrp = new ArrayList[queslist.size() / 2];
^^^^^^^^^^^^^^^^^^^
但您尝试在第二个循环中填充queslist.size()
个:
for (int i = 0; i <= queslist.size()-1; i++) {
^^^^^^^^^^^^^^^^^
resgrp[i].add(queslist.get(i));
^
创建更多数组列表,或者不尝试访问这么多列表: - )