java - how to Add ArrayList in ArrayList -


i got problem when insert arraylist arraylist.

my source code:

import java.util.arraylist;  public class ask {     public static void main(string[] args) {                   arraylist<string> mentah = new arraylist<string>();            mentah.add("reza");           mentah.add("fata");           mentah.add("faldy");           mentah.add("helsan");           mentah.add("dimas");           mentah.add("mamun");           mentah.add("erik");           mentah.add("babeh");           mentah.add("tio");           mentah.add("mamang");            arraylist<arraylist<string>> result =new arraylist<arraylist<string>>();            result.add(mentah);        } } 

how can create list based on data; like:

[[data1,data2,data3],[data4,data5,data6],[data7,data8,data9,data10]] 

10 div 3 3 (so 3 elements per sublist)

10 mod 3 1 (so last sublist has 4 entries)

10 divide 3 3 3 4

just upgraded answer of @narayana ganesh:

arraylist<string> mentah = new arraylist<string>(); mentah.add("reza"); mentah.add("fata"); mentah.add("faldy"); mentah.add("helsan"); mentah.add("dimas"); mentah.add("mamun"); mentah.add("erik"); mentah.add("babeh"); mentah.add("tio"); mentah.add("mamang");  list<list<string>> result = new arraylist<list<string>>();  (int j= 0; j< mentah.size() ; j+=3) {     int end = mentah.size() <= j+2 ? mentah.size() : j+3;     if(mentah.size() - j == 4) end = end +1;     if(j != 9) result.add(mentah.sublist(j, end)); }     system.out.println(result);   } 

result:
[[reza, fata, faldy], [helsan, dimas, mamun], [erik, babeh, tio, mamang]]


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -