
- Java Programming Examples
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Tutorial
- Java - Tutorial
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use method overloading for printing different types of array in Java
Problem Description
How to use method overloading for printing different types of array?
Solution
This example displays the way of using overloaded method for printing types of array (integer, double and character).
public class MainClass { public static void printArray(Integer[] inputArray) { for (Integer element : inputArray){ System.out.printf("%s ", element); System.out.println(); } } public static void printArray(Double[] inputArray) { for (Double element : inputArray){ System.out.printf("%s ", element); System.out.println(); } } public static void printArray(Character[] inputArray) { for (Character element : inputArray){ System.out.printf("%s ", element); System.out.println(); } } public static void main(String args[]) { Integer[] integerArray = { 1, 2, 3, 4, 5, 6 }; Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 }; Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' }; System.out.println("Array integerArray contains:"); printArray(integerArray); System.out.println("\nArray doubleArray contains:"); printArray(doubleArray); System.out.println("\nArray characterArray contains:"); printArray(characterArray); } }
Result
The above code sample will produce the following result.
Array integerArray contains: 1 2 3 4 5 6 Array doubleArray contains: 1.1 2.2 3.3 4.4 5.5 6.6 7.7 Array characterArray contains: H E L L O
java_methods.htm
Advertisements