0

I want to initialize two dimensional array in constructor. But, I have a problem when I declare instance variable of array in class. It will be error if I make it like this:

public class Data {
private String [][] tabel;
public Data(){
    tabel = {{"ID", "NAME"},
             {"101", "Max"},
             {"102", "Mark"},
             {"103", "Downey"},
             {"104", "Matthew"},
             {"105", "Richard"}};
}

How I can solve this problem?

3
  • 1
    add new String[] before each {} block Commented Mar 23, 2017 at 14:24
  • If you don't do it on the same line as the declaration, you need to use new. Commented Mar 23, 2017 at 14:25
  • 1
    What's the error? Commented Mar 23, 2017 at 14:25

1 Answer 1

4

You need to write new Type[] in front of the array initializers like so:

tabel = new String[][]{
            new String[]{"ID", "NAME"},
            new String[]{"101", "Max"},
            new String[]{"102", "Mark"},
            new String[]{"103", "Downey"},
            new String[]{"104", "Matthew"},
            new String[]{"105", "Richard"}};
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.