6

I don't understand the behaviour of Java about arrays. It forbids to define an array in one case but allows the same definition in another.

The example from tutorial:

String[][] names = {
        {"Mr. ", "Mrs. ", "Ms. "},
        {"Smith", "Jones"}
    };
System.out.println(names[0][0] + names[1][0]);    // the output is "Mr. Smith";

My example:

public class User {
   private static String[][] users;
   private static int UC = 0;

   public void addUser (String email, String name, String pass) {
      int i = 0;

      // Here, when I define an array this way, it has no errors in NetBeans
      String[][] u = { {email, name, pass}, {"[email protected]", "jack sparrow", "12345"} };

      // But when I try to define like this, using static variable users declared above, NetBeans throws errors
      if (users == null) {
         users = { { email, name, pass }, {"one", "two", "three"} };    // NetBeans even doesn't recognize arguments 'email', 'name', 'pass' here. Why?

         // only this way works
         users = new String[3][3];
         users[i][i] = email;
         users[i][i+1] = name;
         users[i][i+2] = pass;
         UC = UC + 1;
      }
    }

The mistakes thrown by NetBeans are:

illegal start of expression,

";" expected,

not a statement.

And also it doesn't recognize arguments email, name, pass in the definition of the users array. But recognizes them when I define u array.

What is the difference between these two definitions? Why the one works but the another one defined the same way doesn't?

4 Answers 4

12

You need to add new String[][] before the array aggregate:

users = new String[][] { { email, name, pass }, {"one", "two", "three"} };
Sign up to request clarification or add additional context in comments.

2 Comments

But I have done this already. Right below the line of "public class User {" is the line "private static String[][] users"
@Green The new String[][] part is implied when you combine initialization and declaration; when you declare a field or a variable in one place, but initialize it in a different place, you need to explicitly provide the type of the object that you are creating before the array aggregate.
6

You can use this syntax:

String[][] u = {{email, name, pass}, {"[email protected]", "jack sparrow", "12345"}};

Only when you're declaring the matrix for the first time. It won't work for assigning values to a String[][] variable after you've declared it elsewhere, that's why users = ... fails. For assigning values to an already-declared String[][] (or a matrix of any other type for that matter), use

users = new String[][] { { email, name, pass }, {"one", "two", "three"} };

Comments

2

The first case is an initialization statement, while the second is only an assignment. That kind of filling arrays is only supported when defining a new array.

Comments

2

For re-assigning the matrix you must use new:

users = new String[][] {{email, name, pass }, {"one", "two", "three"}};

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.