Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a multidimensional array in Java using curly braces?

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?

like image 691
Green Avatar asked May 22 '12 18:05

Green


People also ask

What are {} used for in Java?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

How are curly braces used for an array?

{} (curly braces) Define the beginning and end of functions blocks and statement blocks such as the for and if structures. Curly braces are also used for defining initial values in array declarations.

How do you define a multidimensional array?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.


2 Answers

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

users = new String[][] { { email, name, pass }, {"one", "two", "three"} };
like image 166
Sergey Kalinichenko Avatar answered Nov 07 '22 07:11

Sergey Kalinichenko


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"} };
like image 31
Óscar López Avatar answered Nov 07 '22 08:11

Óscar López