Develop a Java program for sorting an integer array, floating-point array and character array. Use Generic method for the implementation. Finally print the sorted array. Note: consider the size of the array to be 5.
Question
Develop a Java program for sorting an integer array, floating-point array and character array. Use Generic method for the implementation. Finally print the sorted array. Note: consider the size of the array to be 5.
Solution
Here is a Java program that uses a generic method to sort an integer array, a floating-point array, and a character array.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Integer[] intArray = {2, 4, 3, 1, 5};
Double[] doubleArray = {1.1, 3.3, 2.2, 5.5, 4.4};
Character[] charArray = {'b', 'a', 'e', 'd', 'c'};
System.out.println("Original arrays:");
printArray(intArray);
printArray(doubleArray);
printArray(charArray);
sortArray(intArray);
sortArray(doubleArray);
sortArray(charArray);
System.out.println("Sorted arrays:");
printArray(intArray);
printArray(doubleArray);
printArray(charArray);
}
public static <T extends Comparable<T>> void sortArray(T[] array) {
Arrays.sort(array);
}
public static <T> void printArray(T[] array) {
for (T element : array) {
System.out.print(element + " ");
}
System.out.println();
}
}
This program first defines three arrays: an Integer array, a Double array, and a Character array. It then prints the original arrays, sorts them using the sortArray method, and prints the sorted arrays.
The sortArray method is a generic method that takes an array of any type that extends Comparable. It uses the Arrays.sort method to sort the array.
The printArray method is also a generic method that takes an array of any type. It iterates over the array and prints each element, followed by a space. After printing all elements, it prints a newline.
Similar Questions
Develop a Java program for sorting and reversing an integer array,character array and floating point array.Use Generic method for the implementation.Finally print the sorted and reversed array.TestcaseInput5(No.of elements (common to all types of array))103020402512.413.311.210.214.6AFDSNOutput403025201014.613.312.411.210.2SNFDA
answer this question in simple code for beginners using intellJ: 4. Write a Java program to sort an array of given integers using the Merge Sort Algorithm.
sort a numeric array and a string arrayWrite a Java program to sort a numeric array and a string array.
Design a Java program that allows users to input a series of integers into an array and then sorts the array in ascending order. Implement the sorting algorithm using the bubble sort technique.Write a Java code that accomplishes the following tasks:Prompt the user to enter the number of elements in the array.Prompt the user to input each element of the array.Sort the array in ascending order using the bubble sort algorithm.Display the sorted array.Demonstrate the functionality of your program with a sample input/output scenario.Sample Input12 // No of elements 23 // Elements to store in an array674512900260725434129865Sample Output12 12 23 34 45 54 65 67 72 98 260 900
Write a class to sort the given set of integers in descending order. Include a try block tolocate the array index out of bounds exception and catch it.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.