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 1
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.
Solution 2
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, 7, 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:
Similar Questions
sort a numeric array and a string arrayWrite a Java program to sort a numeric array and a string array.
sort a numeric array and a string arrayWrite a Java program to sort a numeric array and a string array.Constraints:N/AExample:Input:[1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165, 1457, 2456] Output:[1254, 1456, 1456, 1457, 1458, 1472, 1789, 1899, 2013, 2035, 2165, 2365, 2456, 2458]
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.
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
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.
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.