Split an array list in Java

Posted on 2006-05-02
Last Modified: 2010-05-18
I have an arraylist of >1000 elements.
How do I split it 0-900 in one and the rest in another?

I know I can do a For loop and split it ,.. Is there any other way?
Question by:skn73
    LVL 25

    Expert Comment

    This is probably less memory-efficient — but probably a better solution in general.

    ArrayList list = new ArrayList() ;
    list.put( new String( "Hello" ) ) ;  // or whatever
    // if list.size() > 900
    String [] block = new String [900] ;  // 0-899
    String [] rest   = new Stirng [list.size()-900] ;

    String [] array = (String [] ) list.toArray() ;

    System.arraycopy( array, 0, block, 0, 900 ) ;
    System.arraycopy( rest, 900, rest, 0, list.size()-900 ) ;
    LVL 23

    Expert Comment

    I would use ArrayList.clone() followed by ArrayList.removeRange() on each of the two lists.

    The clone() method makes a shallow copy of the array list, so creates a copy of the list, but does not copy the objects within the list.  [ In other words, the two lists each point to all the same object instances. ]  Use the removeRange() method to remove the first 1000 elements from one list and the post-1000 elements from the other list.  I like this approach because the items contained within the list do not get duplicated in the process.  

    However, it would be naive to assume that iteration does not occur.  It just gets pushed from your code into the methods of ArrayList.
    LVL 5

    Expert Comment

    ArrayList list = new ArrayList();
    list.add(new Long(0));
    list.add(new Long(1));
    list.add(new Long(2));
    list.add(new Long(3));
    list.add(new Long(4));
    list.add(new Long(5));
    ArrayList list1 = new ArrayList(list.subList(0, 3));
    ArrayList list2 = new ArrayList(list.subList(3, 6));

    this code will print -
    [0, 1, 2]
    [3, 4, 5]


    Author Comment

    The List could be 3000 or 4000 so I need chunks of 900
    LVL 5

    Accepted Solution

    If the output list can be multiple then make an array of ArrayLists. calculate the count required by didviding the total by 900. Add in loop that steps 900.  i.e. lists[i] = new ArrayList(list.subList(x , x + 900)); i++; x += 900;


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Suggested Solutions

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now