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 Array in Java

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

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

Printing 1D Arrays

1. Using Arrays.toString()

You can use Arrays.toString() method to print an array. You can simply pass the array reference as an argument to this method to display all the elements of specified array.

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(array));
}
}

    Output:

    [1, 2, 3, 4, 5]

    2. Using a Loop

    One of the easiest way of displaying all the elements of an array is using a loop. In this program, we are using a for-each loop to print an array.

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

    Output:

    1 2 3 4 5

    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.

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

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

    Iterator<Integer> iterator = Arrays.asList(array).iterator();
    while (iterator.hasNext()) {
    System.out.print(iterator.next() + " ");
    }
    System.out.println();
    }
    }

    Output:

    1 2 3 4 5

    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};
    Arrays.stream(array).forEach(element -> System.out.print(element + " "));
    System.out.println();
    }
    }

    Output:

    1 2 3 4 5

    5. Using Arrays.asList

    Arrays.asList() is a method in Java that provides an easy way to convert an array into a fixed-size list. 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};
    List<Integer> list = Arrays.asList(array);
    System.out.println(list);
    }
    }

    Output:

    1 2 3 4 5

    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. Java Scanner class with examples

    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