27

I'm trying to convert a String[][] to a string for display in a GUI. Here's my code:

String[][] tableBornes = SearchPropertiesPlugin.FillBornesTable();
String table = tableBornes.toString();

Here's what it produces:

[[Ljava.lang.String;@19b8858

How can I get something more readable?

1
  • 4
    You're getting the reference of the array. You should iterate through all the elements there and concatenate them to get your desired output. Commented Mar 25, 2013 at 15:37

4 Answers 4

34

try one line version

Arrays.deepToString(tableBornes);

or multiline version

StringBuilder sb = new StringBuilder();
for(String[] s1 : tableBornes){
    sb.append(Arrays.toString(s2)).append('\n');
}
String s = sb.toString();
Sign up to request clarification or add additional context in comments.

1 Comment

thnks, i tried this, but i want also to return in line, to print each list in one line ? how to do this?
21

If you want to create one-line representation of array you can use Arrays.deepToString.


In case you want to create multi-line representation you will probably need to iterate over all rows and append result of Array.toString(array[row]) like

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

String lineSeparator = System.lineSeparator();
StringBuilder sb = new StringBuilder();

for (String[] row : array) {
    sb.append(Arrays.toString(row))
      .append(lineSeparator);
}

String result = sb.toString();

Since Java 8 you can even use StringJoiner with will automatically add delimiter for you:

StringJoiner sj = new StringJoiner(System.lineSeparator());
for (String[] row : array) {
    sj.add(Arrays.toString(row));
}
String result = sj.toString();

or using streams

String result = Arrays
        .stream(array)
        .map(Arrays::toString) 
        .collect(Collectors.joining(System.lineSeparator()));

Comments

6

Try Arrays.toString(Object[] a)

for(String[] a : tableBornes) 
      System.out.println(Arrays.toString(a));

The above code will print every array in two dimensional array tableBornes in newline.

Comments

3

How about this:

StringBuilder sb = new StringBuilder();
for(String[] s1 : tableBornes){
    for(String s2 : s1){
        sb.append(s2);
    }
}
String table = sb.toString();

If you want to insert spaces or some other character between the items just add another append call within the inner loop.

1 Comment

yes i want to add space and also return to the line so as to have the model of the table. am wondring if there's a way to print the table directly since i have the model ?

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.