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

6
  • size of array is required... Commented Jul 31, 2014 at 5:42
  • Even with String[][] data=new String [5][5]; Does not work Commented Jul 31, 2014 at 5:43
  • Please post the whole error message. Also post more of the code so we get context about its location. Commented Jul 31, 2014 at 5:45
  • what do ya mean Does not work? Commented Jul 31, 2014 at 5:45
  • Ok , I got it, it does not work when it is put inside a class, but why? Commented Jul 31, 2014 at 5:47

3 Answers 3

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];
Sign up to request clarification or add additional context in comments.

3 Comments

They aren't columns? What would be a more appropriate name?
String[][] data=new String [5][5]; , even if I add it, it does not work.
@Christian may be "depth" or "size" ?
0

Works perfectly after specifying dimensions for array:

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.