build a matrix with different length at each row ArrayList[] row = new ArrayList[rowNum]; for(int i=0;i<rowNum;i++){ row[i] = new ArrayList<>(); row[i].add(a); row[i].add(b); } Just give an example. This example for each row just "a" and "b". By your talent, you can make variant of it and make each row has different length. Below is from a good link http://stackoverflow.com/questions/10043209/convert-arraylist-into-2d-array-containing-varying-lengths-of-arrays, which is for converting ArrayList into 2D array containing varying lengths of arrays. Which can be a complementary work for mine.
String[][] array =newString[arrayList.size()][]; for(int i =0; i < arrayList.size(); i++){ ArrayList<String> row = arrayList.get(i); array[i]= row.toArray(newString[row.size()]); }