Arrays 使用嵌套for each循环在toString()中打印二维数组

Arrays 使用嵌套for each循环在toString()中打印二维数组,arrays,multidimensional-array,foreach,tostring,Arrays,Multidimensional Array,Foreach,Tostring,因此,我使用Java编写了一个2D数组,在每个插槽中存储歌曲的名称。我必须使用toString()方法打印这些歌曲,并为每个循环嵌套。我不知道该怎么做。帮忙 public class Jukebox { public Jukebox() { String[][] songList = new String[2][3]; for ( String[] row : songList) {

因此,我使用Java编写了一个2D数组,在每个插槽中存储歌曲的名称。我必须使用toString()方法打印这些歌曲,并为每个循环嵌套。我不知道该怎么做。帮忙

public class Jukebox
{
    public Jukebox()
        {
            String[][] songList = new String[2][3];
            for ( String[] row : songList)
                {
                    for ( String column : row)
                        {
                            songList[0][0] = new String( "Hello" );
                            songList[0][1] = new String( "On My Mind" );
                            songList[0][2] = new String( "Hotel Ceiling" );
                            songList[0][3] = new String( "I Wish" );
                            songList[1][0] = new String( "No Air" );
                            songList[1][1] = new String( "Monsters" );
                            songList[1][2] = new String( "Not Afraid" );
                            songList[1][3] = new String( "Wake Up" );
                            songList[2][0] = new String( "Model" );
                            songList[2][1] = new String( "Thank You" );
                            songList[2][2] = new String( "Apologize" );
                            songList[2][3] = new String( "Fireflies" );
                        }
                }
        }

    public String toString()
        {
             for ( String[] row1 : songList)
                {
                    for ( String column1 : row)
                        {
                           System.out.print(  column1 + " " );
                        }
                    System.out.println( "\n" );
                }
        }
}

只需做如下操作:

for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        System.out.print(songList[i][j] + " ");
    }
    System.out.println();
}
for(int i=0;i<2;i++)
{
对于(int j=0;j<3;j++)
{
System.out.print(歌曲列表[i][j]+“”);
}
System.out.println();
}