Java divides the List set into n arrays of a certain capacity.

Source: Internet
Author: User

Recently, when I was working on a project for mass mailing, I had to send a certain amount of addresses in a dark box, but the total number of mails sent each time was not fixed. Finally, I took the time to create a List set to convert a certain number of arrays, sharing Out, I hope everyone will give optimization and I think it is too cool to get the Java code: public List <Object []> listToArray (List <Object> list, int bccSize) {List <Object []> list2 = new ArrayList <Object []> (); Object arr [] = null; for (int j = 1, len = list. size () + 1; j <len; j ++) {if (j = 1) {if (len> bccSize) {// initialize array size arr = new Object [bccSize];} else {arr = new Object [len-1];} arr [j-(bccSize * list2.size ()) -1] = list. get (J-1); // assign the array if (j % bccSize = 0) {list2.add (arr ); // if (len-j-1> bccSize) {arr = new Object [bccSize]; // do not allow null values in the array to create the size of the last array (you can remove it if all values are required)} else {arr = new Object [len-(bccSize * list2.size ()) -1] ;}} else if (j = len-1) {list2.add (arr); // The Last array may not have a specified size} return list2 ;}

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.