Arvind
Arvind

Reputation: 6474

java- how to convert a ArrayList<ArrayList<String>> into String[]][]

I want to store some data in an ArrayList<ArrayList<String>> variable into a csv file.

For this purpose, I zeroed in on Ostermiller Utilities- which include a CSV Writer as well.

The problem is, the csvwrite functionality requires a String, String[] or a String[][] variable.

I wont know beforehand the number of rows/columns in my ArrayList of arraylists-- so how do I use the above (cswrite) functionality? Dont I have to declare a fixed size for a String[]][] variable?

Upvotes: 0

Views: 1187

Answers (4)

Alex
Alex

Reputation: 25613

You can create an array of arrays (matrix) with one size at first, and then iterate and add the data as you traverse the list of list

String[][] arr = new String[listOfList.size()][];
int i = 0;
for (List<String> row: listOfList) {
    arr[i++] = row.toArray(new String[row.size()]);
}

Upvotes: 0

Jochen.Kohler
Jochen.Kohler

Reputation: 82

Here is an Example of how to convert your multidimesional ArrayList into a multidimensional String Array.

package stuff;

import java.util.ArrayList;

public class ArrayTest {

public static void main(String[] args) throws Exception {
    ArrayList<ArrayList<String>> multidimesionalArrayList = createArrayListContent(); 
    String[][] multidimensionalStringArray = new String[multidimesionalArrayList.size()][];
    int index = 0;

    for (ArrayList<String> strings : multidimesionalArrayList) {
        multidimensionalStringArray[index] = strings.toArray(new String[]{}); 
        index++;
    }

    System.out.println(multidimensionalStringArray);
}

private static ArrayList<ArrayList<String>> createArrayListContent() throws Exception  {
    ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
    result.add(createArrayList());
    result.add(createArrayList());
    result.add(createArrayList());
    result.add(createArrayList());
    result.add(createArrayList());
    return result;
}

private static ArrayList<String> createArrayList() throws Exception {
    ArrayList<String> list = new ArrayList<String>();
    list.add(String.valueOf(System.currentTimeMillis()));
    Thread.sleep(10);
    list.add(String.valueOf(System.currentTimeMillis()));
    Thread.sleep(10);
    list.add(String.valueOf(System.currentTimeMillis()));
    Thread.sleep(10);
    list.add(String.valueOf(System.currentTimeMillis()));
    Thread.sleep(10);
    list.add(String.valueOf(System.currentTimeMillis()));
    return list;
}

}

Upvotes: 0

mvmn
mvmn

Reputation: 4047

Tested and working:

        String[][] arrayOfArraysOfString = new String[arrayListOfArrayListsOfStrings.size()][];

    for (int index = 0; index < arrayListOfArrayListsOfStrings.size(); index++) {
        ArrayList<String> arrayListOfString = arrayListOfArrayListsOfStrings.get(index);
        if (arrayListOfString != null) {
            arrayOfArraysOfString[index] = arrayListOfString.toArray(new String[arrayListOfString.size()]);
        }
    }

Upvotes: 0

corsiKa
corsiKa

Reputation: 82559

A String[][] is nothing more than an array of arrays. For example, this makes a 'triangular matrix' using a 2d array. It doesn't have to be a square (although CSV probably should be square, it doesn't have to be).

String[][] matrix = new String[][5];    

matrix[0] = new String[1];
matrix[1] = new String[2];
matrix[2] = new String[3];
matrix[3] = new String[4];
matrix[4] = new String[5];

So for your purposes

String[][] toMatrix(ArrayList<ArrayList<String>> listOFLists) {
    String[][] matrix = new String[][listOfLists.size()];
    for(int i = 0; i < matrix.length; i++) {
        matrix[i]= listOfLists.get(i).toArray();
    }
    return matrix;
}

Just keep in mind that in this case, it's in matrix[col][row], not matrix[row][col]. You may need to transpose this result, depending on the needs of your library.

Upvotes: 3

Related Questions