Java Program to Print Multiple Types of Arrays Using Method Overloading

In the previous article, we have seen Java Program to Find the Sum of First N Elements of the Array

In this article we are going to see how to print multiple types of arrays using method overloading in Java.

Java Program to Print Multiple Types of Arrays Using Method Overloading

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways how to print multiple types of arrays using method overloading.

In Java, method overloading can be defined as a 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 method are different. Means same method name with different signature. We can print different types of arrays using method overloading in java by making sure that the method contains different types of parameters with the same name of the method.

Method-1: Java Program to Print Multiple Types of Arrays Using Method Overloading By Static Initialization of Array Elements

Approach: 

  1. Create four different methods with same name printArray but with different data types of the method arguments.

Program:

public class Main
{

    // creating a method for printing integer
    // array with integer parameter
    public static void printArray(Integer[] arr)
    {
        System.out.println("\nThe 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 + " ");
    }

    public static void main(String args[])
    {

        // calling the parameters of all the
        // methods and taking the inputs
        Integer[] int_arr = { 12, 43, 56, 67, 78, 89 };
        Character[] char_arr = { 'H', 'E', 'L', 'L', 'O' };
        String[] str_arr
            = { "BtechGeeks","is" ,"best", "platform", "to", "learn" };
        Double[] dbl_arr
            = { 23.43, 45.67, 56.78, 67.89 };

        // calling the methods and printing the arrays
        printArray(int_arr);
        printArray(char_arr);
        printArray(str_arr);
        printArray(dbl_arr);
    }
}

Output:

The Integer array is: 
12 43 56 67 78 89

The Character array is: 
H E L L O

The String array is: 
BtechGeeks is best platform to learn

The Double array is: 
23.43 45.67 56.78 67.89

Method-2: Java Program to Print Multiple Types of Arrays Using Method Overloading By Dynamic Initialization of Array Elements

Approach: 

  • Create scanner class object.
  • For each type of array:
    • Ask user for input of length of the array.
    • Initialize the array with given size.
    • Ask the user for input of array elements.
  • Create four different methods with same name printArray but with different data types of the method arguments.

Program:

import java.util.Scanner;

public class Main 
{

    public static void main(String args[]) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);

        //input for integer array
        System.out.print("Enter the size of the integer array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] int_arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            int_arr[i] = sc.nextInt();
        }
        
        // input for String array
        System.out.print("Enter the size String array of array: ");
        int m = sc.nextInt();
        // initialize array with size m
        String[] str_arr = new String[m];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < m; i++) 
        {
            str_arr[i] = sc.nextLine();
        }
        
        // input for Double array
        System.out.print("Enter the size of double array: ");
        int d = sc.nextInt();
        // initialize array with size n
        Double[] dbl_arr = new Double[d];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < d; i++) 
        {
            dbl_arr[i] = sc.nextDouble();
        }
        
        // input for Character array
        System.out.print("Enter the size char of array: ");
        int c = sc.nextInt();
        // initialize array with size n
        char[] char_arr = new char[c];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < c; i++) 
        {
            char_arr[i] = sc.next().toCharArray()[0];
        }

        // calling the methods and printing the arrays
        printArray(int_arr);
        printArray(char_arr);
        printArray(str_arr);
        printArray(dbl_arr);
    }

    // creating a method for printing integer
    // array with integer parameter
    public static void printArray(int[] int_arr) 
    {
        System.out.print("\nThe Integer array is: ");

        // for loop for printing the elements of array
        for (Integer i : int_arr)
            System.out.print(i + " ");
    }

    // overloading the above created method with different
    // parameter method contains a character parameter
    public static void printArray(char[] arr) {
        System.out.print("\nThe Character array is: ");

        // for loop for printing the elements of array
        for (Character i : arr)
            System.out.print(i + " ");
    }

    // now the parameter for the overloaded method is String
    public static void printArray(String[] arr) 
    {
        System.out.print("\nThe String array is: ");

        // for loop for printing the elements of array
        for (String i : arr)
            System.out.print(i + " ");
    }

    // now the parameter for the overloaded method is double
    public static void printArray(Double[] arr) 
    {
        System.out.print("\nThe Double array is: ");

        // for loop for printing the elements of array
        for (Double i : arr)
            System.out.print(i + " ");
    }
}

Output:

Enter the size of the integer array: 3
Enter array elements: 1 2 3
Enter the size String array of array: 2
Enter array elements: aa bb
Enter the size of double array: 3
Enter array elements: 2.2 5.675 65.45
Enter the size char of array: 4
Enter array elements: a b c d

The Integer array is: 1 2 3 
The Character array is: a b c d 
The String array is: aa bb 
The Double array is: 2.2 5.675 65.45

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: