So the idea is to have a parent ArrayList of ArrayList. Every time you call the function, pass along the reference to previous results so they can be consolidated. This is just one out of many ways this can be achieved, but should give you a starting point to optimize further.
// Initialize and provide dummy input.
int requiredIndex = 5;
ArrayList<String> results = new ArrayList<String>();
csvToArray("some,column,data,with,seven,columns,only", results, requiredIndex);
csvToArray("more,data,that,came, in, just, now , and is, longer", results, requiredIndex);
csvToArray("a, short, line", results, requiredIndex);
csvToArray("last, line, of, input, which, is, correct.", results, requiredIndex);
System.out.println(results);
// output : [columns, just, is]
// Update the method as below.
public static void csvToArray(String csvData, ArrayList<String> results, int index){
if (csvData != null){
String[] splitData = csvData.split(",");
int wordCount = 0;
if(splitData.length < index)
return;
for(int i = 0; i < splitData.length; i++){
if(!(splitData[i] == null) || !(splitData[i].length() == 0)) {
ArrayList<String> individualList = new ArrayList<String>();
individualList.add(splitData[i].trim());
if(wordCount == index)
results.add(splitData[i].trim());
wordCount++;
}
}
}
}
As you might notice I have not placed any limitations on number of rows or the number of columns. This is to cater to wider scenarios but if not, you might initialize with limits so memory allocation is optimized.
7
solved Multiple arrays stored in one Variable. [Java]