Java example to print 2d array in string format in console or server logs – using Arrays.deepToString()
and custom methods
Quick Reference:
int [][] cordinates = { {1,2}, {2,4}, {3,6,9} }; System.out.println( Arrays.deepToString(cordinates) ); //[[1, 2], [2, 4], [3, 6, 9]]
1) Arrays.deepToString()
Use deepToString()
method to get string representation of the “deep contents” of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed to convert multi-dimensional arrays to strings.
import java.util.Arrays; public class Main { public static void main(String[] args) { int [][] cordinates = { {1,2}, {2,4}, {3,6,9} }; System.out.println(Arrays.deepToString(cordinates)); } } //Output: [[1, 2], [2, 4], [3, 6, 9]]
2) Custom Method
Use given print2DArray()
to print 2d arrays in custom format which may not be possible with default deepToString()
method. It uses StringBuilder
object to build the string representation of array.
Feel free to customize the method as per your requirements.
import java.util.Arrays; public class TwoDimensionalArrayExamples { public static void main(String[] args) { int [][] cordinates = { {1,2}, {2,4}, {3,6,9} }; System.out.println( print2DArray(cordinates) ); } public static String print2DArray(int arr[][]) { StringBuilder builder = new StringBuilder(); //Open bracket builder.append("["); // Loop through all rows and print for (int i = 0; i < arr.length; i++) { builder.append(Arrays.toString(arr[i]) + ", "); } //Delete last two characters builder.deleteCharAt(builder.length()-1); builder.deleteCharAt(builder.length()-1); //Close bracket builder.append("]"); return builder.toString(); } } //Output: [[1, 2], [2, 4], [3, 6, 9]]
It is very simple method and very handy when you want to print 2d arrays in java.
Happy Learning !!
Reference:
Arrays.toString() JavaDoc
Arrays.deepToString() JavaDoc
kon
//output
[[1, 2], [2, 4], [3, 6, 9]]