Syntax error to make 2D array.

Asked 2 years ago, Updated 2 years ago, 36 views

int[][] multD = new int[5][];
multD[0] = new int[10];

인터넷에서 5x10배열 만드는 소스라고 이런 소스를 봤는데 문법에러가 납니다. Why is that?

java multidimensional-array

2022-09-21 15:52

1 Answers

int[][] multi = new int[5][10]; Do it like this.

Or

int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];

You can also do this. Also, if you want to initialize all the elements,

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

You can do this.


2022-09-21 15:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.