Java Array splitting multiple sub-arrays

Source: Internet
Author: User

Here is a Java collection/array to split multiple sub-collections/arrays of two implementations of the notes.


Way One:

/** *  Split collection  *  @param  datas *  @param  splitSize *  @param   <T> *  @return  */private <t> list<list<t>> splicearrays ( list<t> datas, int splitsize)  {    if  (datas ==  null | | &NBSP;SPLITSIZE&NBSP;&LT;&NBSP;1)  {        return  null ;     }    int totalsize = datas.size ();     //gets the number of molecular arrays to be split     int count =  (totalsize % splitsize  == 0)  ?             (totalsize  / splitsize)  :  (totalsize/splitsize+1);     system.out.println ("Split  count =  " +count");     list<list<t>> rows =  new arraYlist ();    for  (int i = 0;i < count;i ++)  {         int index = i * splitSize;         list<t> cols = new arraylist ();         int j = 0;         while  (j < splitsize && index < totalsize)  {             cols.add (Datas.get (index++));             j ++;         }        rows.add (cols);    }     return rows;}


Way two:

/** *  Split collection  *  @param  datas *  @param  splitSize *  @param   <T> *  @return  */private <t> list<list<t>> splicearrays ( list<t> datas, int splitsize)  {    if  (datas ==  null | | &NBSP;SPLITSIZE&NBSP;&LT;&NBSP;1)  {        return  null ;     }    int totalsize = datas.size ();     int count =  (totalsize % splitsize == 0)  ?              (totalsize / splitsize)  :  ( TOTALSIZE/SPLITSIZE+1);    list<list<t>> rows = new  Arraylist<> ();    for  (int i = 0; i < count;i+ +) &NBSP;{&NBSP;&NBSp;      list<t> cols = datas.sublist (i *  splitsize,                 (i  == count - 1)  ? totalSize : splitSize *  (i + 1));         rows.add (cols);         system.out.println (cols);     }    return rows;}


This article is from the "Cocosdev" blog, make sure to keep this source http://xuzhiwei.blog.51cto.com/978424/1740417

Java Array splitting multiple sub-arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.