"Java" divides the list into several tool classes of List 1. Divided by target number 2. By Target capacity

Source: Internet
Author: User

Title, the sample code is as follows:

/*** 1> by number of copies---list *@paramSource *@paramHow many copies num wants to divide into *@return     */     Public Static<T> list<list<t>> Splitlistfornum (list<t> source,intnum) {List<List<T>> result=NewArraylist<list<t>>(); intRemaider=source.size ()%num;//(the remainder is calculated first)        intNumber=source.size ()/num;//then the business        intoffset=0;//Offset Amount         for(inti=0;i<num;i++) {List<T> value=NULL; if(remaider>0) {Value=source.sublist (I*number+offset, (i+1) *number+offset+1); Remaider--; Offset++; }Else{Value=source.sublist (I*number+offset, (i+1) *number+offset);        } result.add (value); }        returnresult; }    /*** 2> According to target capacity list *@paramSource *@paramcapacity dividing the completed single list capacity *@param<T> *@return     */     Public Static<T> list<list<t>> splitlistbycapacity (list<t> source,intcapacity) {List<List<T>> result=NewArraylist<list<t>>(); if(Source! =NULL){            intSize =source.size (); if(Size > 0 ){                 for(inti = 0; I <size;) {List<T> value =NULL; intEnd = i+capacity; if(End >size) {End=size; } Value=source.sublist (i,end); I=end;                Result.add (value); }            }Else{result=NULL; }        }Else{result=NULL; }        returnresult; }     Public Static voidMain (string[] args) {List<Integer> all =NewArraylist<>(); intA= 1000;  for(inti = 0; i < A; i++) {All.add (i+1); } List<List<Integer>> split = splitlistbycapacity (all,333);        System.out.println (Split.size ());  for(list<integer>strings:split)        {System.out.println (Strings.size ()); }    }

"Java" divides the list into several tool classes of List 1. Divided by target number 2. By Target capacity

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.