1

I'm new to Java.

String[][] data = new String[][];
data[0][0] = "Hello";

This does not work, so can anyone explain why and how to make it work? Well, in C++/Cli this works perfectly but not in Java.

It says:

cannot find symbol: class data

0xCursor
  • 2,242
  • 4
  • 15
  • 33
user3345850
  • 141
  • 2
  • 11

3 Answers3

3

You have to specify the number of rows and columns of the array while declaring it:

String[][] data = new String[2][3];

This will initialize an array with 2 rows and 3 columns. In general:

String[][] data = new String[rows][columns];

You can also ommit the number of columns:

String[][] data = new String[2][];

but to be able to fill it, you will have to initialize each row separately:

String[][] data = new String[2][];
data[0] = new String[3];
data[1] = new String[3];
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

While declaring Array, You need to give dimensions.

For example...

String[][] data=new String [rows][cloumns];

where rows and columns are integers.

for One dimension array

String[] data = new String[size];

PS.

This question may be helpful : Creating Two-Dimensional Array

Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80
0

Works perfectly after specifying dimensions for array:

String[][] data=new String [10][10];
data[0][0]="Hello";
vadimvolk
  • 711
  • 4
  • 15