简体   繁体   English

蚀给令牌上的错误

[英]eclipse giving error on token

I was writing a basic program on 2d array, just defining and initializing. 我在2D数组上写一个基本程序,只是定义和初始化。

 1  package testing;
 2
 3  public class Array2 {
 4    int [][] twoDim = new int[4][];
 5    twoDim[0] = new int[]{1,2,3};
 6    System.out.println(twoDim[0][1]) ;
 7  }

But I get error on line 3 at the semicolon saying that: 但是我在分号的第3行出现了错误:

Syntax error on token ";", { expected after this token

What's wrong? 怎么了?

You need to put your code into somewhere it can be executed. 您需要将代码放入可以执行的位置。 System.out.println is an execution statement. System.out.println是一条执行语句。 You're probably looking to use the main method. 您可能正在寻找使用main方法。

public class Array2 {
    public static void main(String[] args){
        int [][] twoDim = new int[4][];
        twoDim[0] = new int[]{1,2,3};
        System.out.println(twoDim[0][1]) ;
    }
}

Note: You could utilize a combination of: methods, constructors, static initializers, class declarations, etc. to make this properly execute. 注意:您可以使用以下方法的组合:方法,构造函数,静态初始化程序,类声明等,以使其正确执行。 The main method seemed the most appropriate for what you're trying to do. 主要方法似乎最适合您要执行的操作。


To answer your question in the comments of "How can I make the array a class variable". 在“如何使数组成为类变量”的注释中回答您的问题。

You can make twoDim a class variable. 您可以将twoDim类变量。 I would use a Constructor to set the values inside the array. 我将使用Constructor来设置数组内的值。 In your main method you would have to create an instance of your class so that you can access its members. 在您的main方法中,您将必须创建类的实例,以便可以访问其成员。 Also note that the constructor is called when creating an instance of the class. 还要注意,在创建类的实例时会调用构造函数。 For example: 例如:

public class Array2 {
    public int [][] twoDim = new int[4][];

    public Array2(){ // Constructor for Array2 class
        twoDim[0] = new int[]{1,2,3}; // Set the values
    }

    public static void main(String[] args){
        Array2 array2Instance = new Array2(); // Create an instance, calls constructor
        System.out.println(array2Instance.twoDim[0][1]); // Access the instance's array
    }
}

Note that you have to make the twoDim variable public in order to access it outside of the class - for instance in the main method. 请注意,必须将twoDim变量twoDim public ,以便在类外部访问它-例如在main方法中。

you cannot write 你不能写

System.out.println(twoDim[0][1]);

outside a method or block in java This probably should do the job 在Java中的方法或块之外这可能应该可以完成工作

public static void main(String arr[])
{
    int [][] twoDim = new int[4][];
    twoDim[0] = new int[]{1,2,3};
    System.out.println(twoDim[0][1]) ;

}

or something like this 或类似的东西

int [][] twoDim = new int[4][];
public void display()
{
    twoDim[0] = new int[]{1,2,3};
    System.out.println(twoDim[0][1]) ;
}


public static void main(String arr[])
{
    new Array2().display();

}

You can initialize an array inside a method, or code block. 您可以在方法或代码块中初始化数组。

System.out.println(twoDim[0][1]) ;

This statement should be inside some block or method. 该语句应在某些块或方法内。 You cannot have it like that lying in the class. 你不能像在课堂上那样拥有它。 Put it inside some block or method. 将其放在某些块或方法中。

If I don't write System.out.println(....), the error still persists. 如果我不写System.out.println(....),该错误仍然存​​在。

You cannot initialize elements of an array after defining it that way. 这样定义数组后,就不能对其进行初始化。 You need to put the statement 您需要发表声明

twoDim[0] = new int[]{1,2,3};

inside block/method/constructor. 内部块/方法/构造函数。

Same goes for any array 任何数组都一样

Eg 例如

public class HelloWorld {
    String[] strArray =new String[10];  //all good
    strArray[0] ="str";   //same compile time error you are getting
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM