Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sublist Arraylist in android

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);
        }
    }
like image 240
Shruti Avatar asked Mar 02 '12 12:03

Shruti


2 Answers

Short Answer

I'd suggest you use either:

  • Google Guava's Lists.partition(List, int)
  • or the JDK's List.subList(int, int)

Long Answer + Examples

Use Google Guava's Lists.partition(List, int)

It will divide your List in Lists 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:

  • It uses List.subList internally (see below).
  • It returns a view! (you might want to make copies).

Use the JDK's Standard 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'll need to calculate the sizes yourself (Guava alleviates that).
    • Save yourself the boilerplate and use Guava.
  • It returns a view! (you might want to make copies).

Additional Reading and Other Methods

  • Vogella's article on Splitting / Partitioning A Java Collection
  • Subdividing a Collection into Smaller Collections
like image 111
haylem Avatar answered Sep 29 '22 03:09

haylem


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 :-)

like image 41
aioobe Avatar answered Sep 29 '22 02:09

aioobe