In Java method overloading can be defined as the class containing multiple methods with the same name but the list of parameters or type of parameters or the order of the parameters of the methods should not be the same. We can print different types of arrays using method overloading in java by making sure that the method contains different parameters with the same name of the method.
Let’s implement a program with the printArray() method overloaded three times to print different types of array. And every time the function has different parameters. In the main method provide the required inputs to each array and then by calling their respective methods the array printed on the screen.
Implementation:
Java
// Java Program to Use Method Overloading// for Printing Different Types of Arrayclass methodOverloadingDemo {Â
    // creating a method for printing integer    // array with integer parameter    public static void printArray(Integer[] arr)    {        System.out.println("The Integer array is: ");Â
        // for loop for printing the elements of array        for (Integer i : arr)            System.out.print(i + " ");        System.out.println();    }Â
    // overloading the above created method with different    // parameter method contains a character parameter    public static void printArray(Character[] arr)    {        System.out.println("\nThe Character array is: ");Â
        // for loop for printing the elements of array        for (Character i : arr)            System.out.print(i + " ");        System.out.println();    }Â
    // now the parameter for the overloaded method is String    public static void printArray(String[] arr)    {        System.out.println("\nThe String array is: ");Â
        // for loop for printing the elements of array        for (String i : arr)            System.out.print(i + " ");        System.out.println();    }Â
    // now the parameter for the overloaded method is double    public static void printArray(Double[] arr)    {        System.out.println("\nThe Double array is: ");Â
        // for loop for printing the elements of array        for (Double i : arr)            System.out.print(i + " ");    }Â
    // main function    public static void main(String args[])    {Â
        // calling the parameters of all the        // methods and taking the inputs        Integer[] iarr = { 2, 4, 6, 6, 8 };        Character[] carr = { 'H', 'E', 'L', 'L', 'O' };        String[] sarr            = { "Ram", "Nidhi", "John", "Raju", "Sara" };        Double[] darr            = { 10.0123, 89.123, 65.132, 78.321, 1.798 };Â
        // calling the methods and printing the arrays        printArray(iarr);        printArray(carr);        printArray(sarr);        printArray(darr);    }} |
Â
Â
The Integer array is: 2 4 6 6 8 The Character array is: H E L L O The String array is: Ram Nidhi John Raju Sara The Double array is: 10.0123 89.123 65.132 78.321 1.798
Â
Time Complexity: O(N), where N is the size of the array.
Auxiliary space: O(1) because constant variables have been used
Â

… [Trackback]
[…] Read More on to that Topic: geeksforgeeks.org/java-program-to-use-method-overloading-for-printing-different-types-of-array/ […]
… [Trackback]
[…] Find More Information here on that Topic: geeksforgeeks.org/java-program-to-use-method-overloading-for-printing-different-types-of-array/ […]