Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:45:11+00:00 2026-05-30T16:45:11+00:00

I want to sublist an arraylist and store it in small arraylists. I am

  • 0

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);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:45:13+00:00Added an answer on May 30, 2026 at 4:45 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get a sublist of a List but I want the
Want the function to sort the table by HP but if duplicate HPs then
want to ask user to input something but not want to wait forever. There
I want to write a function that determines if a sublist exists in a
I want to DeleteDuplicates from a list of lists keeping the sublist structure intact.
I have a list of lists (sublist) that contains numbers and I only want
I have the following list (it’s a length 2 list, but in my assignment
In Java, i want to get subList from a list. i cannot use subList
I want to delete a sublist from a list. I only know the starting
Want to know what the stackoverflow community feels about the various free and non-free

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.