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);
}
}
Short Answer
I’d suggest you use either:
Lists.partition(List, int)List.subList(int, int)Long Answer + Examples
Use Google Guava‘s
Lists.partition(List, int)It will divide your
ListinLists of the specifiedintsize:Notes:
List.subListinternally (see below).Use the JDK’s Standard
List.subList(int, int)Notes:
Additional Reading and Other Methods