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]
Question
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]
Solution 1
Here is a Java program that sorts a numeric array and a string array:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Numeric Array
int[] numericArray = {1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165, 1457, 2456};
Arrays.sort(numericArray);
System.out.println("Numeric Array: " + Arrays.toString(numericArray));
// String Array
String[] stringArray = {"Java", "Python", "C++", "C#", "JavaScript", "Ruby", "Swift", "Go"};
Arrays.sort(stringArray);
System.out.println("String Array: " + Arrays.toString(stringArray));
}
}
In this program, we first import the java.util.Arrays package which provides methods to manipulate arrays.
In the main method, we define a numeric array and a string array. We then use the Arrays.sort() method to sort these arrays.
Finally, we print the sorted arrays using Arrays.toString() method which returns a string representation of the array.
Solution 2
Here is a Java program that sorts a numeric array and a string array:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Numeric Array
int[] numericArray = {1789, 2035, 1899, 1456, 2013, 1458, 2458, 1254, 1472, 2365, 1456, 2165, 1457, 2456};
Arrays.sort(numericArray);
System.out.println("Numeric Array: " + Arrays.toString(numericArray));
// String Array
String[] stringArray = {"Java", "Python", "C++", "JavaScript", "Ruby", "Swift"};
Arrays.sort(stringArray);
System.out.println("String Array: " + Arrays.toString(stringArray));
}
}
In this program, we first import the java.util.Arrays package which provides methods to manipulate arrays.
In the main method, we define a numeric array and a string array. We then use the Arrays.sort() method to sort these arrays.
Finally, we print the sorted arrays using Arrays.toString() method which returns a string representation of the array.
Similar Questions
sort a numeric array and a string arrayWrite a Java program to sort a numeric array and a string array.
Write a java program to implement interfaces sort the set of numbers and a set of strings in a given 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
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.
Language: Javapublic int[] sort(int[] numbers, int length)if (length < 2) {}return numbers;int mid length / 2;6int[] left = new int[id]:7int[] right new int[length mid]:9for (int i=0; i<mid; i++) {1011}1213left[i] numbers[i];for (int i-mid; i < length; i++) {INSERT MISSING CODE14}151617sort(left, mid);sort(right, length - mid);1819return merge(numbers, left, right, mid, length - mid);20}Image not displaying?Complete the sort function by replacing // INSERT MISSING CODE with the correct line of code.
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.