Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»JAVA»5 Methods to Print an Array in Java

    5 Methods to Print an Array in Java

    RahulBy RahulMarch 30, 20222 Mins ReadUpdated:April 21, 2022

    An array is a data structure to store multiple elements of similar data types. Similar to other programming languages Java also supports Arrays. An Array is always stored in a contiguous location on the system memory. Java provides multiple methods of printing an Array basis on the requirements. We can directly access any array of elements with the index number or print the entire Array using Java loops.

    In this tutorial, you’ll learn different techniques to print the elements of a given array in Java.

      1. Arrays.toString() method
      2. Arrays.deepToString() method
      3. for Loop
      4. for-each Loop
      5. Arrays.asList() method

    Let’s discuss the above methods one by one including examples.

    1. Java Arrays.toString() Method

    The Arrays.toString() method is the simplest and frequently used method to print an Array in Java programming.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import java.util.Arrays;
     
    public class ArrayExample1 {
     
        public static void main(String[] args) {
            // Initialize an array
            String[] array = {"apple","orange","banana","grapes","mango"};
     
            // Print array elements
            System.out.println(Arrays.toString(array));
        }
    }

    Save above java program in ArrayExample1.java file and compile it and run. You will see the output like below:

    Output:
    [apple, orange, banana, grapes, mango]

    2. Using Arrays.deepToString() Method

    Java Arrays.deepToString() method is used to fetch the deep content from an array. Here the deep content represents the multidimensional arrays.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import java.util.Arrays;
     
    public class ArrayExample2 {
     
        public static void main(String[] args) {
            //declaration and initialization of a multidimensional array  
            int[][] array = {{1,2,3}, {4, 5}, {6, 7, 8}};
     
            // Print array elements
            System.out.println(Arrays.deepToString(array));
        }
    }

    Save above java program in ArrayExample2.java file and compile it and run. You will see the below output:

    Output:
    [[1, 2, 3], [4, 5], [6, 7, 8]]

    3. Using for Loop

    The for loop is a frequently used method used for integrations. We can navigate through all array elements by the array index number. The following example will print array elements using for loop in Java.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public class ArrayExample3 {
     
        public static void main(String[] args) {
            //declaration and initialization of an array
            String[] array = {"apple","orange","banana","grapes","mango"};
     
            // Print array elements
            for(int i=0;i<array.length;i++)
                System.out.println(array[i]);  
        }
    }

    Save above java program in ArrayExample3.java file and compile it and run. You will see the below output:

    Output:
    apple orange banana grapes mango

    4. Using for-each Loop

    For-each is another way for traversing an array than for loop. Here is a quick example of using for-each to get array elements and print them.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public class ArrayExample4 {
     
        public static void main(String[] args) {
            //declaration and initialization of an array
            String[] array = {"apple","orange","banana","grapes","mango"};
     
            // Print array elements
            for (String element: array) {
                System.out.println(element);
            }
        }
    }

    Save program in a file named ArrayExample4.java. Then compile the program and run it. You will see the below output:

    Output:
    apple orange banana grapes mango

    5. Using Arrays.asList() Method

    The asList() is another java function that returns a fixed-size list of a specified array.

    Java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import java.util.Arrays;  
    public class ArrayExample5
    {
        public static void main(String [] args)  
        {
            //declaration and initialization of an array
            String[] array = {"apple","orange","banana","grapes","mango"};
     
            // Print array elements
            System.out.println(Arrays.asList(array));
        }
    }

    Save java program to file named ArrayExample5.java, then compile and run it. You will see the below output:

    Output:
    [apple, orange, banana, grapes, mango]

    Wrap Up

    In this tutorial, you have learned 5 Java methods to print an array including examples.

    array
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleAdding a New SSH Key in GitHub
    Next Article Java HashMap – How to Get Key from Value

    Related Posts

    How to Install JAVA on Ubuntu 22.04

    Updated:May 16, 20224 Mins Read

    Java HashMap – How to Get Value from Key

    Updated:April 6, 20222 Mins Read

    Java HashMap – How to Get Key from Value

    Updated:April 6, 20222 Mins Read

    How To Install Java on Debian 11

    4 Mins Read

    How to Check Java Version

    2 Mins Read

    How to Check If a Value Exists in An Array in PHP

    1 Min Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    • How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.