1

I try to learn about array types. Following code I used for multi dimensional array and It did't give correct result set. It gave users=[[Ljava.lang.String;@2d68be1b as output. What are the changes I need to do?

import java.util.ArrayList;

    public class Array_MultiDimentional2 {
        public static void main(String[] args) {
            String[][] users;
            String email = null;
            String name = null;
            String pass = null;
            users = new String[][] {{email, name, pass }, {"one", "two", "three"}};
            System.out.println("users=" + users);

        }
    }
1
  • 1
    If you want to store structured data (like pairs of email, name, pass) in Java, then declaring a class is recommended. It makes your code compile safe and helps to keep the code readable. Commented Mar 25, 2016 at 5:14

4 Answers 4

4

Dynamic way to Print Your Array

Java has a nice utility class called Arrays which contains many handy methods for dealing with arrays. One of them is called deepToString, which is what we'll use:

import java.util.ArrayList;
import java.util.Arrays;

public class Array_MultiDimentional2 {
    public static void main(String[] args) {
        String[][] users;
        String email = null;
        String name = null;
        String pass = null;
        users = new String[][] {{"email", "name", "pass" }, {"one", "two", "three"}};
        //System.out.println("users=" + users);
        System.out.println(Arrays.deepToString(users));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to print a multidimensional array like that, you could write some for cycles like:

for (int i=0; i<users.length; i++) {
   for (int j=0; j<users[i].length; j++) {
      System.out.println(users[i][j]);
   }
}

It is not so optimal, but it could help you.

Comments

1

If you want to print elements in this array, how about to try this one.

for(int i=0; i<users.length; i++){
            for(int j=0; j<users[0].length; j++)
                System.out.print("users=" + users[i][j] + " ");
            System.out.println();
        }

Comments

1

You cannot directly print Array to get it's value(either one-dimensional or multidimensional). You have to print each value contain in the Array. For this example you can use:

for (int i=0; i<users.length; i++) {
   for (int j=0; j<users[i].length; j++) {
      System.out.println(users[i][j]);
   }
}

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.