Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»JAVA»5 Methods to Print an Array in Java

    5 Methods to Print an Array in Java

    By RahulApril 21, 20222 Mins Read

    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.

    Advertisement

    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.

    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.

    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.

    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.

    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.

    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

    Related Posts

    What is the Access Modifiers in Java

    What are the Access Modifiers in Java

    Installing Java on Ubuntu 22.04

    How to Install JAVA on Ubuntu 22.04

    How to Get Value from Key in Java HashMap

    Java HashMap – How to Get Value from Key

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    • sleep Command in Linux with Examples
    • 20 Basic Linux Commands for the Beginners (Recommended)
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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