0

I would like to export a multidimensional array to a CSV file. I'm able to get a set array to export, but am struggling with a growing array

Here is what I have so far:

String[][] data = new String[][] {
    new String[]{"quick","brown","fox"},
    new String[]{"apple","banana","cherry"},
    new String[]{"quick","brown","fox"}
};

for(int i=0; i<data.length; i++) {
        //first line prints "quick", "brown", "fox" etc
            writer.writeNext(data[i]);
        }

Now growing from my code, I need to be able to add arrays to "data".

For instance, if I have:

String[] abc = new String[] {"a","b","c"};
String[] def = new String[] {"d","e","f"};
...

and I don't know how many of these string arrays I'll have, researching tells me that I'll need to add these to a ArrayList. So I came up with something like this:

        List<String[]> stockList = new ArrayList<String[]>();
        stockList.add(abc);
        stockList.add(def);
        // could potentially add more

        String[] stockArr = new String[stockList.size()];
        stockArr = stockList.toArray(stockArr);

        for(int i=0; i<data.length; i++) {
            writer.writeNext(stockArr[i]);
            Log.i("loop", "here: " + data[i]);
        }

except writeNext cannot be applied to a java string.

Overall, it seems like I need to have an ArrayList of a string arrays. Then at the end, convert that ArrayList into an Array and not lose the string arrays?

Would appreciate any help, thanks!

1 Answer 1

1

Have you tried this:

for(String[] arr:stockList) {
    writer.writeNext(arr);
    Log.i("loop", "here: " + arr);
}

Better still :

writer.writeAll(stockList);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.