3
public static void main{
    String [][] book = new String[100][6];

    for(int i = 0; i < 1; i++) {
        for(int j = 0; j < 5; j++) {
            book[i][j] = i;
        }
    }

    arrayMethod(book);
}

public static void arrayMethod(String[][] array){
    System.out.println(Arrays.asList(array));
}

arrayMethod method output is [[Ljava.lang.String;@639facbc, [Ljava.lang.String;@8059dbd, [Ljava.lang.String;@28b6e768, [Ljava.lang.String;@1271ba, ....

Problem is that in arrayMethod I can't acces 2 dimension array data, where can be problem?

2
  • to compile you have to use book[i][j] =""+ i; Commented Dec 2, 2013 at 13:38
  • What about the signature of main method ?? Commented Dec 2, 2013 at 13:51

4 Answers 4

4

It's doing exactly what you want: you're pretending the (first-level) array is a List (of Array) and then printing the toString() of those, which looks something like [Ljava.lang.String@pointer. You probably want this instead:

System.out.println(Arrays.deepToString(array));
Sign up to request clarification or add additional context in comments.

Comments

2

as Alya'a Gamal said, if you want to put an int inside an array of String you need to parse it : book[i][j] = Integer.toString(i);. Then if you want to display your array, you need to run thought it, like this for example :

public static void arrayMethod(String[][] array){
        for(int i = 0; i < array.length;i++) {
            for(int j = 0; j < array[i].length;j++)
                System.out.println(array[i][j]); // a stringBuilder would be better than to print inside the loop
        }

    }

1 Comment

Have one problem, passing only 1 (last) array element, all oher elements is null
2

You can use Arrays.toString to print 1-D string array, but you CANNOT use Arrays.toString to print the 2-D array directly.

There are 2 ways for you to print the 2D string array in console.

Way#1 ==>

System.out.println(Arrays.deepToString(array));

Way#2 ==>

 for(String[] arr: array)
 {
        System.out.println(Arrays.toString(arr));
 }

Comments

2

I see three issues here :

(1). The signature of the main method looks odd. It would raise a compile issue.

public static void main(String args[])
{
// Your code here
}

(2). In the following code :

for(int i = 0; i < 1; i++) {
  for(int j = 0; j < 5; j++) {
            book[i][j] = i;
        }
    }

book[i][j] =i; // Here you are trying to insert an int in place where a String is required.

This will again lead to a compile time issue.

You can correct it as follows:

 book[i][j] = Integer.toString(i);

(3).

Use the following static method in the Arrays class to print the elements of 2D array on the console.

System.out.println(Arrays.deepToString(array));

Hope this helps.

+1 for isolating the problem.

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.