I want to sublist an arraylist and store it in small arraylists.
I am doing as following but unable to enter exact values in the small arrays.Please see my code and suggest some other way or tell me what i am doing wrong
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() + "");
}
EDIT :
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);
}
}
I'd suggest you use either:
Lists.partition(List, int)
List.subList(int, int)
Lists.partition(List, int)
It will divide your List
in List
s of the specified int
size:
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
Notes:
List.subList
internally (see below).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));
}
}
Notes:
You're creating queslist.size() / 2
lists to store the "sublists"
ArrayList[] resgrp = new ArrayList[queslist.size() / 2];
^^^^^^^^^^^^^^^^^^^
but you try to populate up to queslist.size()
of them in the second loop:
for (int i = 0; i <= queslist.size()-1; i++) {
^^^^^^^^^^^^^^^^^
resgrp[i].add(queslist.get(i));
^
Either create more array lists, or don't try to access so many of them :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With