0

I define a two dimension string object and then try to print it but I don't get the string printed. Can someone tell me what I am missing here?

String[][] input = { { "a", "b", "c" }, { "a", "b", "a" } };

System.out.println(input);

Output:

[[Ljava.lang.String;@6475d174

Ypnypn
  • 933
  • 1
  • 8
  • 21
curiousJ
  • 203
  • 1
  • 4
  • 11
  • @BrianRoach That question is about printing a 1D array, and its solution, `Arrays.toString`, is not sufficiently deep for the 2D array here. – rgettman Jan 13 '14 at 23:35
  • @rgettman ignoring that it explains *why* they're getting the output, if you'd like I could spend another 5 seconds and find one specific to 2d arrays. – Brian Roach Jan 13 '14 at 23:36
  • @BrianRoach If you find one with 2+ D arrays, then I'll help you mark this as a duplicate of it (and delete my answer here). – rgettman Jan 13 '14 at 23:38
  • 1
    http://stackoverflow.com/questions/14544623/2d-array-output-is-no-where-near-correct/14544659#14544659 - I lied, took me prob 20 seconds :) – Brian Roach Jan 13 '14 at 23:39
  • The first dup. actually does have an answer about multi-dimensional arrays. – Reinstate Monica -- notmaynard Jan 13 '14 at 23:40
  • Thanks guys but I wish you hadn't down voted the question. I copied the exact error in search but couldn't find a solution. – curiousJ Jan 13 '14 at 23:43
  • btw this works: System.out.println(Arrays.deepToString(input)); – curiousJ Jan 14 '14 at 00:14

2 Answers2

1

You're not fully understanding the array aspect of this...

int rowIndex, colIndex;
String[][] input = { { "a", "b", "c" }, { "a", "b", "a" } };
System.out.println(input[rowIndex][colIndex]);

//If you want to traverse through the entire 2-D array 
//all you will need to do is use two for loops
Tdorno
  • 1,571
  • 4
  • 22
  • 32
1
for(int i = 0; i < input.length; i++)
    for(int j = 0; j < input[i].length; j++)
        System.out.print(input[i][j]);
danihodovic
  • 1,151
  • 3
  • 18
  • 28