实例化2D数组

时间:2017-05-09 06:46:44

标签: java arrays

我想制作2d数组并稍后解析它:

String[][] position  = new String[3][3]; 
    position = [{id:10, x:10, y:20}, {id:5, x:30, y:40}, {id:2, x:2, y:7},];

String[][] position  = [
    {id:10, x:10, y:20}, 
    {id:5, x:30, y:40},
    {id:2, x:2, y:7},
    ];

但这两个人都没有工作......

关于我做错了什么的任何建议?

3 个答案:

答案 0 :(得分:4)

First String不是int,String应该在""之间,你的赋值应该是这样的:

String[][] position  = 
                 {{"id:10", "x:10", "y:20"}, {"id:5", "x:30", "y:40"}, {"2", "2", "7"}};

如果你的意思是int数组,你的数组应该是这样的:

int[][] position  = {{10, 10, 20}, {5, 30, 40}, {2, 2, 7}};

答案 1 :(得分:3)

你可以对数组进行常量初始化

如何:

[] 替换为 {} 并放置有效字符串,因为这是您的数组所持有的

String[][] position  = {  {"id:10", "x:10", "y:20"}, 
{"id:5", "x:30", "y:40"},
{"id:2", "x:2", "y:7"} };

请注意,这是一个特殊的init只有在声明完成后才有效...意味着以下内容无效:

String[][] position;
position  = {  {"id:10", "x:10", "y:20"},     {"id:5", "x:30", "y:40"},
{"id:2", "x:2", "y:7"} };

答案 2 :(得分:0)

试试这个

int[][] multi = new int[3][3];

int[] twoDimIntArray[] = new int[3][3];

这相当于

int[][] multi = new int[][]{
  { 0, 0, 0 },
  { 0, 0, 0 },
  { 0, 0, 0 },
  { 0, 0, 0 },
  { 0, 0, 0 }
};

初始化后,您可以输入值