用我的2D数组java代码找不到错误

时间:2016-01-14 05:53:50

标签: java arrays multidimensional-array error-handling syntax-error

Java表示存在预期的“]”错误。我找不到我的代码有什么问题? Song类是另一个类组合类,没有问题。提前感谢您的帮助

public class Jukebox
{
    Song[][] songList;

    songList[0][0] = new Song("Jet Airliner", 5 );

    songList[0][1] = new Song( "Slide", 4 );

    songList[0][2] = new Song( "Tom Sawyer", 3 );
    songList[0][3] = new Song( "Purple Rain", 2 );
    songList[1][0] = new Song( "Sing a Song", 1 );
    songList[1][1] = new Song( "Baba O'Riley", 5 );
    songList[1][2] = new Song( "Jumper", 4 );
    songList[1][3] = new Song( "Car Wash", 3 );
    songList[2][0] = new Song( "Kung Fu Fighting", 2 );
    songList[2][1] = new Song( "Right as Rain", 4 );
    songList[2][2] = new Song( "Beat It", 5 );
    songList[2][3] = new Song( "Bust a Move", 4 );

    public  String toString()
    {
        String output = " ";
        for (int row = 0; row < songList.length; row++)
        {
            for (int col = 0; col < songList[row].length; col++)
            {
                output+=(songList[row][col]);
            }
            output += "\n";
        }
        return output;
    }

}

5 个答案:

答案 0 :(得分:0)

有两个问题

  1. songList需要在放入元素
  2. 之前使用数组的大小进行初始化
  3. songList [x] [x]的值应该在构造函数或方法中设置。
  4. public class Jukebox {
    
        Song[][] songList = new Song[3][4];
    
        public Jukebox() {
            songList[0][0] = new Song("Jet Airliner", 5);
            songList[0][1] = new Song("Slide", 4);
            songList[0][2] = new Song("Tom Sawyer", 3);
            songList[0][3] = new Song("Purple Rain", 2);
            songList[1][0] = new Song("Sing a Song", 1);
            songList[1][1] = new Song("Baba O'Riley", 5);
            songList[1][2] = new Song("Jumper", 4);
            songList[1][3] = new Song("Car Wash", 3);
            songList[2][0] = new Song("Kung Fu Fighting", 2);
            songList[2][1] = new Song("Right as Rain", 4);
            songList[2][2] = new Song("Beat It", 5);
            songList[2][3] = new Song("Bust a Move", 4);
        }
    
        public String toString() {
            String output = " ";
            for (int row = 0; row < songList.length; row++) {
                for (int col = 0; col < songList[row].length; col++) {
                    output += (songList[row][col]);
                }
                output += "\n";
            }
            return output;
        }
        public static void main(String[] args) {
            Jukebox j = new Jukebox();
        }
    }
    

    歌曲

    class Song {
        Song(String name, int i) {
        }
    }
    

答案 1 :(得分:0)

您需要在方法中执行以下所有操作:

songList[0][0] = new Song("Jet Airliner", 5 );

songList[0][1] = new Song( "Slide", 4 );

songList[0][2] = new Song( "Tom Sawyer", 3 );
songList[0][3] = new Song( "Purple Rain", 2 );
songList[1][0] = new Song( "Sing a Song", 1 );
songList[1][1] = new Song( "Baba O'Riley", 5 );
songList[1][2] = new Song( "Jumper", 4 );
songList[1][3] = new Song( "Car Wash", 3 );
songList[2][0] = new Song( "Kung Fu Fighting", 2 );
songList[2][1] = new Song( "Right as Rain", 4 );
songList[2][2] = new Song( "Beat It", 5 );
songList[2][3] = new Song( "Bust a Move", 4 );

所以你创建一个init()方法,或者如果它适合你的目的就创建一个构造函数。

public void init(){
    songList = new String[3][4];
    songList[0][0] = new Song("Jet Airliner", 5 );

    songList[0][1] = new Song( "Slide", 4 );

    songList[0][2] = new Song( "Tom Sawyer", 3 );
    songList[0][3] = new Song( "Purple Rain", 2 );
    songList[1][0] = new Song( "Sing a Song", 1 );
    songList[1][1] = new Song( "Baba O'Riley", 5 );
    songList[1][2] = new Song( "Jumper", 4 );
    songList[1][3] = new Song( "Car Wash", 3 );
    songList[2][0] = new Song( "Kung Fu Fighting", 2 );
    songList[2][1] = new Song( "Right as Rain", 4 );
    songList[2][2] = new Song( "Beat It", 5 );
    songList[2][3] = new Song( "Bust a Move", 4 );
}

现在,在调用toString()的{​​{1}}方法之前,您需要调用JukeBox方法。

答案 2 :(得分:0)

尝试这个

android:layout_gravity="end"

答案 3 :(得分:0)

这个程序在语法上是错误的 1. Song[][] songList;必须由new运营商陪同 2.在实例级别,成员只能在一个语句中初始化。所以下面是错误的:

Song[][] songList;
songList[0][0] = new Song("Jet Airliner", 5 ); 
songList[0][1] = new Song( "Slide", 4 ); 
songList[0][2] = new Song( "Tom Sawyer", 3 ); 
songList[0][3] = new Song( "Purple Rain", 2 ); 
songList[1][0] = new Song( "Sing a Song", 1 ); 
songList[1][1] = new Song( "Baba O'Riley", 5 ); 
songList[1][2] = new Song( "Jumper", 4 ); 
songList[1][3] = new Song( "Car Wash", 3 ); 
songList[2][0] = new Song( "Kung Fu Fighting", 2 ); 
songList[2][1] = new Song( "Right as Rain", 4 ); 
songList[2][2] = new Song( "Beat It", 5 ); 
songList[2][3] = new Song( "Bust a Move", 4 );

解决方案:
1.使用new运算符实例化数组 Song[][] songList = new Song[3][4];

  1. 使用初始化程序块或构造函数将对象分配给数组索引。

    class Jukebox { Song[][] songList = new Song[3][4]; { //INIT Block for assignments } }

  2. 除#2外,您可以使用数组初始值设定项。

    Song[][] songList = new Song[][]{{}{}{}}

答案 4 :(得分:0)

在Java中,如果要为类属性赋值,则应在声明它后立即执行。

而不是这样做:

Song[][] songList;

    songList[0][0] = new Song("Jet Airliner", 5 );

    songList[0][1] = new Song( "Slide", 4 );

    songList[0][2] = new Song( "Tom Sawyer", 3 );
    songList[0][3] = new Song( "Purple Rain", 2 );
    songList[1][0] = new Song( "Sing a Song", 1 );
    songList[1][1] = new Song( "Baba O'Riley", 5 );
    songList[1][2] = new Song( "Jumper", 4 );
    songList[1][3] = new Song( "Car Wash", 3 );
    songList[2][0] = new Song( "Kung Fu Fighting", 2 );
    songList[2][1] = new Song( "Right as Rain", 4 );
    songList[2][2] = new Song( "Beat It", 5 );
    songList[2][3] = new Song( "Bust a Move", 4 );

试试这个:

Song[][] songList = {
        {
            new Song("Jet Airliner", 5 ),
            new Song( "Slide", 4 ),
            new Song( "Tom Sawyer", 3 ),
            new Song( "Purple Rain", 2 )
        },
        {
            ...
        }

    };

或者,您可以在构造函数中进行赋值。

public Jukebox(){
songList = {
            {
                new Song("Jet Airliner", 5 ),
                new Song( "Slide", 4 ),
                new Song( "Tom Sawyer", 3 ),
                new Song( "Purple Rain", 2 )
            },
            {
                ...
            }

        };
}