Write a java program to implement interfaces sort the set of numbers and a set of strings in a given array
Question
Write a java program to implement interfaces sort the set of numbers and a set of strings in a given array
Solution
Sure, here is a simple Java program that uses the Comparable
interface to sort an array of numbers and an array of strings:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Integer[] numbers = {5, 2, 8, 3, 1};
String[] strings = {"Apple", "Dog", "Cat", "Elephant", "Ball"};
Arrays.sort(numbers);
Arrays.sort(strings);
System.out.println("Sorted numbers: " + Arrays.toString(numbers));
System.out.println("Sorted strings: " + Arrays.toString(strings));
}
}
In this program, we're using the Arrays.sort()
method to sort the arrays. This method uses the Comparable
interface to compare the elements and sort them.
The Integer
and String
classes in Java already implement the Comparable
interface, so we can use the Arrays.sort()
method directly on arrays of these types.
When you run this program, it will print the sorted arrays of numbers and strings.
Similar Questions
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.
Which Java data structure is used to implement the sorting algorithm without extra space?
Write a Java program to remove the duplicate elements of a given array and return the new length of the array
Which of these interfaces is not a part of Java's collection framework?Options: Pick one correct answer from belowListSetSortedMapSortedList
Write a Java program to segregate all 0s on left side and all 1s on right side of a given array of 0s and 1s
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.