如何将2D ArrayList转换为2D String?

时间:2015-06-16 21:20:56

标签: java arrays netbeans arraylist

我试图转换我的2D ArrayList" dataArrayList"到一个2D字符串数组,以便我可以按字母顺序排序,但它在我尝试时给我一个警告,说:

$sorted

当我尝试运行它时,它失败了。这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

编辑:看起来已经完成了这项工作:Convert ArrayList into 2D array containing varying lengths of arrays

由于您需要将List的两个级别转换为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]);
}

上述相关问题的第二个答案为我上面提到的方法提供了另一种方法:

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()][]);

}

答案 1 :(得分:0)

要将列表转换为二维数组,您必须遍历列表并将每个元素分配给数组。

我在这里为你写一个变压器;

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;
}

打印也类似,只需要遍历数组并格式化输出;

我还写了这个用于打印二维数组的文件;

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();
    }

}

作为一个完整的解决方案,你可以检查一下,它工作正常;

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
    }

}

示例输入与输入文件相同;

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