如何在Java中将String []转换为String [] []?

时间:2011-04-05 18:28:25

标签: java

如何将String []数组转换为String [] []?例如,如果String []是这样的:String [] array = new String [] {“Artist,Song,Genre”};如何将其转换为String [] [],以便“艺术家”在一个“单元格”中,例如String [0] [0] =“Artist”;

谢谢。

2 个答案:

答案 0 :(得分:3)

试试这个:

String[] array = new String[]{"a,b,c", "1,2,3", "x,y,z"};

String[][] twoDim = new String[array.length][];

for (int i = 0; i < twoDim.length; i++)
    twoDim[i] = array[i].split(",");

System.out.println(Arrays.deepToString(twoDim));

打印:

[[a, b, c], [1, 2, 3], [x, y, z]]

或许这就是你所追求的:

String[][] twoDim = { { "Artist1", "Song1", "Genre1" },
                      { "Artist2", "Song2", "Genre2" },
                      { "Artist3", "Song3", "Genre3" } };

答案 1 :(得分:0)

你的意思是:

String[][] array = new String[][] {
    new String[] {
        "Artist", "Genre", "Song"
    },
    new String[] {
        "artist1", "genre1", "song1"
    },
    new String[] {
        "artist2", "genre2", "song2"
    }
};