3

I wish to print the contents of String[][] myArray using only one print statement.

Obviously the array can be printed easily by looping through it, but for the sake of clean log files, I'd like the array cleanly printed out using only one println/logging line, and thus need some way of concatenating the array's contents into one big honker string with appropriately placed newlines.

One could always write a one-line scala function to do this no sweat, but I'd prefer to keep this java only.

Is there a straightforward way to do this?

2 Answers 2

9

A one-line approach:

System.out.println(java.util.Arrays.deepToString(myArray));

Sign up to request clarification or add additional context in comments.

Comments

2

Go with anorton's method.....

=============================

Sure, create a method:

private static final String concatenate(String[][] data) {
    StringBuilder sb = new StringBuilder();
    for (String[] line : data) {
        sb.append(Arrays.toString(line)).append(\n");
    }
    return sb.toString();
}

Then, you can log:

logger.log(concatenate(data));

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.