0

I'm trying to convert my 2D ArrayList "dataArrayList" to a 2D String array so that I can sort it alphabetically, but it's giving me a warning when I try, saying:

Suspicious Collection.toArray() call. Collection item type java.util.List<java.lang.String> 
is not assignable to array component type java.lang.String[]

And when I try to run it, it fails. Here is my code:

public static List<List<String>> dataArrayList = new ArrayList<>();

public static void main(String args[]) {
    try {
        readFile();
        alphaSort();
    } catch (Exception e) {
    }
}

public static void readFile() {
    FileReader fileRead = new FileReader(txtFileDirectory);
    BufferedReader dataReader = new BufferedReader(fileRead);
    String line = "";

    for (int i = 0; line != null; i ++) {
        line = dataReader.readLine();
        if (line != null)
            dataArrayList.add(Arrays.asList(line.split(",");
    }
}

public static void alphaSort() {
    String[][] alphaArray = new String[dataArrayList.Size()][15]

    //warning underline on "alphaArray"
    dataArrayList.toArray(alphaArray);
}
Hunter
  • 13
  • 1
  • 3
  • possible duplicate of [Convert ArrayList into 2D array containing varying lengths of arrays](http://stackoverflow.com/questions/10043209/convert-arraylist-into-2d-array-containing-varying-lengths-of-arrays) – River Jun 16 '15 at 21:47
  • Welp, apparently the mods don't think so. See if my answer below works for you anyways I suppose. (It compiles and seems to work just fine for me.) – River Jun 16 '15 at 22:58

2 Answers2

1

EDIT: looks like this has already been done: Convert ArrayList into 2D array containing varying lengths of arrays.

You are getting an error as you need to convert both levels of List to array. Try looping through your list and convert each sublist to an array.

public static void alphaSort() {
    String[][] alphaArray = new String[dataArrayList.Size()][15];
    for(int i = 0; i < dataArrayList.size(); i++)
        alphaArray[i] = dataArrayList.get(i).toArray(new String[15]);
}

The second answer of the above linked question provides an alternate method to the one I have presented above:

public static void alphaSort() {
    ArrayList<String[]> tempList = new ArrayList<String[]>();
    for (ArrayList<String> stringList : dataArrayList)
        tempList.add(stringList.toArray(new String[stringList.size()]));

    String[][] alphaArray = tempList.toArray(new String[dataArrayList.size()][]);

}
Community
  • 1
  • 1
River
  • 8,585
  • 14
  • 54
  • 67
0

To convert a list to 2-dimensional array you have to traverse inside a list and assign each element to the array.

Here I write a transformer for you;

public static String[][] transformListToArray(List<List<String>> inputList) {

    int rows = inputList.size();
    int columns = inputList.get(0).size();

    String[][] array2d = new String[rows][columns];

    for( int i = 0; i < inputList.size(); i++ )
        for( int j = 0; j < inputList.get(i).size(); j++ )
            array2d[i][j] = inputList.get(i).get(j);

    return array2d;
}

Printing is also similiar, only you have to traverse inside the array and format the output;

I also wrote this one for printing the 2-dimensional array;

public static void print2DArray( String[][] inputArray) {

    for( int i = 0; i < inputArray.length; i++ ) {

        for( int j = 0; j < inputArray[i].length; j++ )
            System.out.printf("%s ", inputArray[i][j]);

        System.out.println();
    }

}

As a complete solution, you can check this out, it's working fine;

public class TestConvert2DArray {

    public static List<List<String>> dataArrayList = new ArrayList<>();

    public static void main(String args[]) {
        try {
            readFile();
            // alphaSort(); we dont need it anymore
            String new2dArray[][] = transformListToArray(dataArrayList);
            print2DArray(new2dArray);

        } catch (Exception e) {
        }
    }

    // To be used on transforming the list to 2d-array
    public static String[][] transformListToArray(List<List<String>> inputList) {

        int rows = inputList.size();
        int columns = inputList.get(0).size();

        String[][] array2d = new String[rows][columns];

        for (int i = 0; i < inputList.size(); i++)
            for (int j = 0; j < inputList.get(i).size(); j++)
                array2d[i][j] = inputList.get(i).get(j);

        return array2d;
    }

    // To be used on printing the 2d-array to the console
    public static void print2DArray(String[][] inputArray) {

        for (int i = 0; i < inputArray.length; i++) {

            for (int j = 0; j < inputArray[i].length; j++)
                System.out.printf("%s ", inputArray[i][j]);

            System.out.println();
        }

    }

    // Just added an init for textFileDirectory
    public static void readFile() throws Exception {
        String txtFileDirectory = "D:\\try.txt"; // "D:\\try.txt"
        FileReader fileRead = new FileReader(txtFileDirectory);
        BufferedReader dataReader = new BufferedReader(fileRead);
        String line = "";

        // And you dont need and index inside the for loop
        for (; line != null;) {
            line = dataReader.readLine();
            if (line != null)
                dataArrayList.add(Arrays.asList(line.split(",")));
        }

        dataReader.close(); // You have to close all the reasources to prevent
                            // data leak
    }

}

A sample input is same with the input file;

01 - What comes around, goes around
02 - Eyes wide open
03 - What comes around, goes around
04 - Eyes wide open
05 - What comes around, goes around
06 - Eyes wide open
07 - What comes around, goes around
08 - Eyes wide open
09 - What comes around, goes around
10 - Eyes wide open
River
  • 8,585
  • 14
  • 54
  • 67
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106