在Android中的子列表Arraylist

时间:2012-03-02 12:39:41

标签: java arraylist sublist

我想将一个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);
        }
    }

2 个答案:

答案 0 :(得分:6)

简答

我建议你使用:

长答案+例子

使用Google GuavaLists.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(见下文)。
  • 它返回一个视图! (你可能想制作副本)。

使用JDK的标准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));
  }

}

备注:

  • 您需要自己计算尺寸(番石榴可以减轻这种情况)。
    • 保存样板并使用Guava。
  • 它返回一个视图! (你可能想制作副本)。

补充阅读和其他方法

答案 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));
           ^

创建更多数组列表,或者不尝试访问这么多列表: - )