简体   繁体   English

在Android中的子列表Arraylist

[英]Sublist Arraylist in android

I want to sublist an arraylist and store it in small arraylists. 我想将一个arraylist子列表并存储在小型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: 我建议你使用:

Long Answer + Examples 长答案+例子

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

It will divide your List in List s of the specified int size: 它将分配你的ListList指定的第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

Notes: 笔记:

  • It uses List.subList internally (see below). List.subList内部使用List.subList (见下文)。
  • It returns a view! 它返回一个视图! (you might want to make copies). (你可能想制作副本)。

Use the JDK's Standard List.subList(int, int) 使用JDK的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. 保存样板并使用Guava。
  • It returns a view! 它返回一个视图! (you might want to make copies). (你可能想制作副本)。

Additional Reading and Other Methods 附加阅读和其他方法

You're creating queslist.size() / 2 lists to store the "sublists" 您正在创建queslist.size() / 2列表来存储“子列表”

ArrayList[] resgrp = new ArrayList[queslist.size() / 2];
                                   ^^^^^^^^^^^^^^^^^^^

but you try to populate up to queslist.size() of them in the second loop: 但是你试着在第二个循环中填充它们的queslist.size()

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 :-) 要么创建更多的数组列表,要么不尝试访问这么多数组:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM