0

What i wanted do is this:

class test{
public static String[][] p;

}

But something is wrong. When i try to write sth to it by using:

p[][]={...};

It says: "p cannot be resolved to a type".

EDIT: Ok, I see there were some problems with understanding what i am talking about so i post a code here.

public class Test{
    static String[][] plansza;
    public static void main(String []arg){

                p[][]={ {"  ","A", "B", "C", "D", "E", "F", "G", "H"},
                  {"1.","0","1","0","1","0","1","0","1"},
                  {"2.","1","0","1","0","1","0","1","0"},
                  {"3.","0","1","0","1","0","1","0","1"},
                  {"4.","0","0","0","0","0","0","0","0"},
                  {"5.","0","0","0","0","0","0","0","0"},
                  {"6.","2","0","2","0","2","0","2","0"},
                  {"7.","0","2","0","2","0","2","0","2"},
                  {"8.","2","0","2","0","2","0","2","0"}
                  };
       }
2
  • 6
    That's not how you "write something to it". Refer to a basic tutorial. Commented Dec 23, 2013 at 14:13
  • 2
    Maybe try p = new String[][]{{...},{...}}; Commented Dec 23, 2013 at 14:13

5 Answers 5

1

To create array normally you need to at least set first dimension of array, but normally you do it like

1) new Type[dim1][dim2]..[dimN]

You can skip dimensions if you initialize arrays with elements like

2) new Type[][]..[]{{..{elements},},..}}

But if you are initializing your array with elements in the same place where you are declaring it then you can skip new Type[][]...[] part and just use {{..{elements},},..}} like

3) Type[][]..[] myArray = {{..{elements},},..}}


In your case you are having case 2) because you are not initializing your array in place you declared it. So instead of

p[][] = {
  {"  ","A","B","C","D","E","F","G","H"},
  ...
  };

you need to write it as

p = new String[][]{
  {"  ","A","B","C","D","E","F","G","H"},
  ...
  };
Sign up to request clarification or add additional context in comments.

Comments

0

Try like the follwoing

public class Test{
static String[][] plansza;
public static void main(String []arg){

            plansza = new String[][] { {"  ","A", "B", "C", "D", "E", "F", "G", "H"},
              {"1.","0","1","0","1","0","1","0","1"},
              {"2.","1","0","1","0","1","0","1","0"},
              {"3.","0","1","0","1","0","1","0","1"},
              {"4.","0","0","0","0","0","0","0","0"},
              {"5.","0","0","0","0","0","0","0","0"},
              {"6.","2","0","2","0","2","0","2","0"},
              {"7.","0","2","0","2","0","2","0","2"},
              {"8.","2","0","2","0","2","0","2","0"}
              };
   }
}

you must place new String[][]{....}; before assign it to plansza

Comments

0

Try this type of initialization:

public static String[][] s = new String[][]{
    {"A", "B", "C"},
    {"D", "E", "F"}
};

This would define dinamically your multidimensional Array

Comments

0

You can only use array constants in initializers.

Therefore:

// will compile
public static String[][] p = {{"foo", "bar"}};

// will compile
public static String[][] p;
static {
    p = new String[][]{{"foo", "bar"}};
}

// won't compile
public static String[][] p;
static {
    p = {{"foo", "bar"}};
}

Comments

0

Understand that p is a variable of type String[][] , thus initialize it in the following way

p = new String[][]{...}; 

In the above code p which is of type String[][] is assigned to an object initialized of the same type.

when you try to assign values, you can specify the indices like this

p[i][j] = "value";

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.