BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to print 2D Array in Java

Last Updated: June 3, 2024 by Chaitanya Singh | Filed Under: java

In Java, there are several ways to print a 2D array. In this guide, we will see various programs to print 2D array using different approaches:

Note: In the previous tutorial, I have covered how to print an array(1D array).

Printing 2D Arrays

1. Using Arrays.deepToString()

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},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(Arrays.deepToString(array));
}
}

Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

2. Using Nested Loop

Another simple way of displaying a 2D array is by using nested loops. In this program, we are using nested for-each loops.

public class Main {
public static void main(String[] args) {
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int[] row : array) {
for (int element : row) {
System.out.print(element + " ");
}
System.out.println();
}
}
}

Output:

1 2 3
4 5 6
7 8 9

3. Using Iterator

Another way to print an array is by converting it into a list and then using an iterator to display the elements of the list. In this example, we are using nested iterators.

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Main {
public static void main(String[] args) {
Integer[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

for (Integer[] row : array) {
List<Integer> list = Arrays.asList(row);
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();
}
}
}

Output:

1 2 3
4 5 6
7 8 9

4. Using Stream

You can use the Stream API to print each element of the array. You can refer Java Stream Tutorial to learn more about Stream.

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

Arrays.stream(array).forEach(row -> {
Arrays.stream(row).forEach(element -> System.out.print(element + " "));
System.out.println();
});
}
}

Output:

1 2 3
4 5 6
7 8 9

5. Using Arrays.asList

For 2D arrays, you can convert each row to a list using asList() method and print it. This method belongs to java.util.Arrays class. I have covered some of the commonly used methods here: Java Arrays Methods.

import java.util.Arrays;
import java.util.List;

public class Main {
public static void main(String[] args) {
Integer[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

for (Integer[] row : array) {
List<Integer> list = Arrays.asList(row);
System.out.println(list);
}
}
}

Output:

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

Top Related Articles:

  1. How to sort Hashtable in java
  2. Java 9 – @SafeVarargs Annotation (with examples)
  3. Java 8 Interface Changes – default method and static method
  4. Java 8 Stream Tutorial
  5. How to print Array in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap