String[] myList = {"a","b","c","d"}; ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>(); data.add(new ArrayList<String>()); int outerIndex =0; int innerIndex =0; for (int i =0; i<list.length; i++) { data.get(outerIndex).add(innerIndex, list[i]); innerIndex++; } System.out.println(data);
A simple loop to add data to a multidimensional array.
For each external index you need to add
data.add(new ArrayList<String>());
then increase the outer index and reset the inner index.
It will look something like this.
public static String[] myList = {"a", "b","-","c","d","-","e","f","-"}; public static ArrayList<ArrayList<String>> splitList(String[] list) { ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>(); data.add(new ArrayList<String>()); int outerIndex =0; int innerIndex =0; for (int i=0; i<list.length; i++) { System.out.println("will add: " + list[i]); if(!list[i].contains("-")) { System.out.println("outerIndex: " + outerIndex +" innerIndex: "+ innerIndex); data.get(outerIndex).add(innerIndex, list[i]); innerIndex++; } else { outerIndex++;
source share