Error in Java array definition assignment

CSDN Q & A 2022-02-13 07:55:14 阅读数:37

error java array definition assignment

public class Map {
int[][] mymap = new int[8][7];
Map(){
mymap= {
{6,3,3,3,3,3,6},//6 Represent grass
{3,3,0,0,0,3,6},//4 End point
{3,1,2,0,0,3,6},//3 Represents a wall
{3,3,2,0,0,3,3},//2 Represents a box
{3,3,0,2,0,0,3},//1 Show people
{3,4,2,0,0,0,3},//0 Representing vacant land
{3,4,4,0,0,4,3},
{3,3,3,3,3,3,3}
};
}
Why report a mistake




Take the answer :

new After that, it is initialized , You can't assign values directly like that anymore , You can declare on it , Then write like this
mymap= new int[][]{
{6,3,3,3,3,3,6},//6 Represent grass
{3,3,0,0,0,3,6},//4 End point
{3,1,2,0,0,3,6},//3 Represents a wall
{3,3,2,0,0,3,3},//2 Represents a box
{3,3,0,2,0,0,3},//1 Show people
{3,4,2,0,0,0,3},//0 Representing vacant land
{3,4,4,0,0,4,3},
{3,3,3,3,3,3,3}
};



Other answers 2:

Yours Map The class name cannot be the same as that of the system Map Class name , Change the class name .

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130755122929.html