You can print in simple way.

Use below to print 2D array

int[][] array = new int[rows][columns];
System.out.println(Arrays.deepToString(array));

Use below to print 1D array

int[] array = new int[size];
System.out.println(Arrays.toString(array));
Answer from Prabhakaran Ramaswamy on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › print-2-d-array-matrix-java
Print a 2D Array or Matrix in Java - GeeksforGeeks
March 24, 2025 - // Java program to print the elements of // a 2 D array or matrix using toString() import java.io.*; import java.util.*; // Driver Class class Geeks { public static void print2D(int mat[][]) { // Loop through all rows for (int[] row : mat) // converting each row as string // and then printing in a separate line System.out.println(Arrays.toString(row)); } public static void main(String args[]) throws IOException { int mat[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; print2D(mat); } } ... Example 4: Using Arrays.deepToString(int[][]) converts the 2D array to a string in a single
Discussions

java - Printing a 2D array - Code Review Stack Exchange
The problem presented to me: Declare a two-dimensional array of ints in main. Write a method to read in the size of each dimension, allocate memory for the array and return it (DON'T DO THIS IN M... More on codereview.stackexchange.com
🌐 codereview.stackexchange.com
October 11, 2012
Print 2D color array with boxes
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
3
1
June 6, 2021
[Java] Printing out a row in a 2d array
That’s how Java prints arrays. You’ll need to wrap the array in Arrays.toString(array) or alternatively loop over the array printing out results More on reddit.com
🌐 r/learnprogramming
2
1
January 7, 2021
Trying to print a 2d array by columns.
That's not how printing works. You can't go back to a previous line to edit once it's already been printed to console. You have to print things by row. More on reddit.com
🌐 r/javahelp
2
3
March 16, 2020
🌐
Baeldung
baeldung.com › home › java › java array › print a java 2d array
Print a Java 2D Array | Baeldung
August 23, 2024 - The Arrays.stream() method can be employed to flatten the 2D array, and then forEach() is used to print the elements. Let’s look into the implementation: int[][] myArray = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; Arrays.stream(myArray) ...
🌐
W3Schools
w3schools.com › java › java_arrays_multi.asp
Java Multi-Dimensional Arrays
Java Wrapper Classes Java Generics Java Annotations Java RegEx Java Threads Java Lambda Java Advanced Sorting ... How Tos Add Two Numbers Swap Two Variables Even or Odd Number Reverse a Number Positive or Negative Square Root Area of Rectangle Celsius to Fahrenheit Sum of Digits Check Armstrong Num Random Number Count Words Count Vowels in a String Remove Vowels Count Digits in a String Reverse a String Palindrome Check Check Anagram Convert String to Array Remove Whitespace Count Character Frequency Sum of Array Elements Find Array Average Sort an Array Find Smallest Element Find Largest Element Second Largest Array Min and Max Array Merge Two Arrays Remove Duplicates Find Duplicates Shuffle an Array Factorial of a Number Fibonacci Sequence Find GCD Check Prime Number ArrayList Loop HashMap Loop Loop Through an Enum
🌐
FavTutor
favtutor.com › blogs › print-2d-array-in-java
Print a 2D Array or Matrix in Java: 4 Easy Methods (with code)
June 16, 2023 - In 2D arrays, arr.length returns the number of rows it has, and performing the same operation on any one of its indices (for example arr[0].length) will return the number of columns in the array. It’s great that now you know how to print matrices in Java, now you should also learn sorting algorithms in Java.
🌐
C# Corner
c-sharpcorner.com › article › printing-a-2d-array-in-java-with-code
Printing a 2D Array in Java with Code
January 23, 2025 - public class Print2DArray { public ... row } } } ... Java provides a built-in method called Arrays.deepToString() that can be used to print multi-dimensional arrays easily....
Find elsewhere
🌐
Ruby-Doc.org
ruby-doc.org › home › two dimensional array in java – the ultimate guide with examples
Two Dimensional Array in Java - The Ultimate Guide with Examples - Ruby-Doc.org
April 10, 2026 - javaCopyEditpublic class JaggedArrayExample { public static void main(String[] args) { int[][] jagged = new int[3][]; jagged[0] = new int[]{1, 2}; jagged[1] = new int[]{3, 4, 5}; jagged[2] = new int[]{6, 7, 8, 9}; for (int i = 0; i < jagged.length; i++) { for (int j = 0; j < jagged[i].length; j++) { System.out.print(jagged[i][j] + " "); } System.out.println(); } } } ... Index Out of Bounds Exceptions: Always check array lengths before accessing. Assuming uniform row sizes: Java allows jagged arrays, so length of each row might differ. Confusing row and column indices: Remember array[row][column]. Two dimensional arrays in Java are a powerful and flexible way to represent tabular data. Whether for mathematical operations, data storage, or grid-based applications, mastering 2D arrays is essential for Java programmers.
🌐
CodeGym
codegym.cc › java blog › java arrays › 2d arrays in java
Java Matrix - 2D Arrays
April 8, 2025 - Here’s another way to print2D arrays in Java using “foreach loop”. This is a special type of loop provided by Java, where the int[]row will loop through each row in the matrix.
🌐
Codecademy
codecademy.com › learn › learn-java › modules › java-two-dimensional-arrays › cheatsheet
Learn Java: Two-Dimensional Arrays Cheatsheet | Codecademy
//Given a 2d array called `arr` which stores `int` values ... In Java, initializer lists can be used to quickly give initial values to 2D arrays.
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › how-to-print-2d-array-in-java
How to print 2D Array in Java
June 3, 2024 - You can use Arrays.deepToString() method to print a 2D array. You can simply pass the reference of 2D array as an argument to this method in order to display all the elements. import java.util.Arrays; public class Main { public static void main(String[] args) { int[][] array = { {1, 2, 3}, ...
🌐
GitHub
gist.github.com › eMahtab › 297419cb805fe93346c0
Java program to take 2D array as input from user. · GitHub
June 11, 2019 - arr[row][col]=s.nextInt(); } } for ( row = 0; row < arr.length; row++) { for (col = 0; col < arr[row].length; col++) { System.out.print(arr[row][col]+" "); } System.out.println(); } } } Copy link · Copy Markdown · import java.io.*; import java.util.Arrays; //package assignments.ass23; import java.util.Scanner; /** 2darray */ public class darray { public static void getArray(int n,int[][] array) { Scanner sc = new Scanner(System.in); array = new int[n][n]; System.out.println("Enter the elements :"); for(int i= 0;i<n;i++){ for(int j = 0;j<n;j++){ array[i][j] = sc.nextInt(); } } } public static
🌐
Google AI
ai.google.dev › gemini api › image understanding
Image understanding | Gemini API | Google AI for Developers
1 week ago - The box_2d should be [ymin, xmin, ymax, xmax] normalized to 0-1000."}, { "type": "image", "uri": "https://example.com/image.png", "mime_type": "image/png" } ], "response_format": { "type": "text", "mime_type": "application/json", "schema": { "type": "object", "properties": { "boxes": { "type": "array", "items": { "type": "object", "properties": { "box_2d": { "type": "array", "items": { "type": "integer" } }, "mask": { "type": "array", "items": { "type": "array", "items": { "type": "integer" } } }, "label": { "type": "string" } }, "required": ["box_2d", "mask", "label"] } } }, "required": ["boxes"] } } }'
🌐
Openai
developers.openai.com › api › docs › guides › embeddings
Vector embeddings | OpenAI API
Article: \"\"\" {wikipedia_article_on_curling} \"\"\" Question: Which athletes won the gold medal in curling at the 2022 Winter Olympics?""" response = client.chat.completions.create( messages=[ {'role': 'system', 'content': 'You answer questions about the 2022 Winter Olympics.'}, {'role': 'user', 'content': query}, ], model=GPT_MODEL, temperature=0, ) print(response.choices[0].message.content)
🌐
Staying
staying.fun › en
Real-time Code Visualization for Python/JS/C++
Step into the world of two-dimensional arrays, where data comes alive in rows and columns. Watch as we traverse matrices, solve grid-based puzzles, and manipulate game boards through interactive visualizations. See how 2D arrays power everything from image processing to tile-based games, making ...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › print-a-given-matrix-in-spiral-form
Print a given matrix in spiral form - GeeksforGeeks
import java.util.ArrayList; class GfG { static ArrayList<Integer> spirallyTraverse(int[][] mat) { int m = mat.length; int n = mat[0].length; ArrayList<Integer> res = new ArrayList<>(); // Initialize boundaries int top = 0, bottom = m - 1, left = 0, right = n - 1; // Iterate until all elements are printed while (top <= bottom && left <= right) { // Print top row from left to right for (int i = left; i <= right; ++i) { res.add(mat[top][i]); } top++; // Print right column from top to bottom for (int i = top; i <= bottom; ++i) { res.add(mat[i][right]); } right--; // Print bottom row from right to
Published   July 17, 2025
🌐
Unstop
unstop.com › home › blog › tcs nqt coding questions 2026 with answers & pyqs
TCS NQT Coding Questions 2026 with Answers & PYQs
March 17, 2026 - import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Read inputs int n = scanner.nextInt(); long x = scanner.nextLong(); int[] ar = new int[n]; for (int i = 0; i < n; i++) { ar[i] = scanner.nextInt(); } // Sort the array Arrays.sort(ar); boolean[] vis = new boolean[n]; int j = 0, q = 0; int ans = 0; for (int i = 0; i < n; i++) { if (vis[i]) { continue; } if (ar[i] * x > ar[j]) { while (j < n && ar[i] * x >= ar[j]) { q = ++j; } q = --j; } if (i < q && ar[i] * x == ar[q]) { vis[q--] = true; } else { ans++; } } System.out.println(ans); } }
🌐
Medium
alokkumar-17171.medium.com › how-to-print-contents-of-2d-array-properly-without-loop-java-a548d723e396
How to print contents of 2D array properly without loop — Java | by Alok Kumar - Software Engineer - OPEN FOR WORK - | Medium
February 11, 2024 - package com.arrays; import java.util.Arrays; public class SortArray { public static void main(String[] args) { // TODO Auto-generated method stub int[][] arr = {{2, 5, 15},{1, 2, 8},{3, 5, 10},{4,4,20}}; Arrays.sort(arr,(x,y)->{ if(x[1]==y[1]) return x[2]-y[2]; return x[1]-y[1]; }); // Output - [[1, 2, 8], [4, 4, 20], [3, 5, 10], [2, 5, 15]] System.out.println(Arrays.deepToString(arr)); // Output - [[I@6ff3c5b5 System.out.println(arr); } } The above issue occurs not only with 2D array but also with normal array. ... package com.arrays; import java.util.Arrays; public class Print1DArray { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = {34,12,11,89,42,67}; // Output - [34, 12, 11, 89, 42, 67] System.out.println(Arrays.toString(arr)); // Output - [I@4dc63996 System.out.println(arr); } }
🌐
Reddit
reddit.com › r/javahelp › print 2d color array with boxes
r/javahelp on Reddit: Print 2D color array with boxes
June 6, 2021 -

Here is my code that I found online.

public static void printMatrix(int size,int row, Color[][] array) {
        for (int i = 0; i < 7 * size; i++) {
          System.out.print("-");
        }
        System.out.println("-");
    
        for (int i = 1; i  <= array[row].length; i++) {
          System.out.printf("| %4d ", array[row][i - 1]);
        }
        System.out.println("|");
    
        if (row == size - 1) {
          for (int i = 0; i < 7 * size; i++) {
            System.out.print("-");
          }
          System.out.println("-");
    
        }
    }

In my main method, I call the printMatrix function and define size, row, and the Color[][] array.

I receive the error:

Exception in thread "main" java.util.IllegalFormatConversionException: d != java.awt.Color  
        at java.base/java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4445)
        at java.base/java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2957)  
        at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2911)
        at java.base/java.util.Formatter.format(Formatter.java:2689)
        at java.base/java.io.PrintStream.format(PrintStream.java:1209)
        at java.base/java.io.PrintStream.printf(PrintStream.java:1105)
        at Main.printMatrix(Main.java:50)
        at Main.main(Main.java:35)

From googling things, I believe the issue is that %4d only works for strings. Is there something else I Can use for all types of array formats?

My end goal is for the output to be like this:

Where x is something like: java.awt.Color[r=255,g=0,b=0] (I'll remove the java.awt.Color part later).
----------------------
|    x |     x |     x |
----------------------
|     x |     x |    x |
----------------------
|     x |     x |    x |
----------------------
Top answer
1 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 3
1
Why can’t u just use a regular print statement and not use printf?
🌐
InterviewBit
interviewbit.com › python-interview-questions
120+ Top Python Interview Questions and Answers (2026) - InterviewBit
import numpy as np one_dimensional_list = [1,2,4] one_dimensional_arr = np.array(one_dimensional_list) print("1D array is : ",one_dimensional_arr) 2D array creation: import numpy as np two_dimensional_list=[[1,2,3],[4,5,6]] two_dimensional_arr = np.array(two_dimensional_list) print("2D array is : ",two_dimensional_arr) 3D array creation: import numpy as np three_dimensional_list=[[[1,2,3],[4,5,6],[7,8,9]]] three_dimensional_arr = np.array(three_dimensional_list) print("3D array is : ",three_dimensional_arr) ND array creation: This can be achieved by giving the ndmin attribute.
Published   January 24, 2026