This Java program is to Implement Sorted array. A sorted array is an array data structure in which each element is sorted in numerical, alphabetical, or some other order, and placed at equally spaced addresses in computer memory.
Here is the source code of the Java program to implement sorted array. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
import java.util.Arrays;
public class SortedArray<T>
{
private T[] array;
public SortedArray(T[] array)
{
this.array = array;
}
public void sort()
{
Arrays.sort(array);
}
public T[] getArray()
{
return array;
}
public static void main(String...arg)
{
Integer[] inums = {10,9,8,7,6};
Float[] fnums = {23.9f,5.5f,10.8f,2.5f,82.0f};
Double[] dnums = {12.5,244.92,1.9,98.3,35.2};
String[] strings = {"banana","pineapple","apple","mango","jackfruit"};
System.out.println("The Values Before sorting");
System.out.println();
System.out.println("Integer Values");
for (int i = 0; i < inums.length; i++)
System.out.print(inums[i] + "\t");
System.out.println();
System.out.println("Floating Values");
for (int i = 0; i < fnums.length; i++)
System.out.print(fnums[i] + "\t");
System.out.println();
System.out.println("Double Values");
for (int i = 0; i < dnums.length; i++)
System.out.print(dnums[i] + "\t");
System.out.println();
System.out.println("String Values");
for (int i = 0; i < strings.length; i++)
System.out.print(strings[i] + "\t");
SortedArray<Integer> integer = new SortedArray<Integer>(inums);
SortedArray<Float> floating = new SortedArray<Float>(fnums);
SortedArray<Double> doubles = new SortedArray<Double>(dnums);
SortedArray<String> string = new SortedArray<String>(strings);
integer.sort();
floating.sort();
doubles.sort();
string.sort();
inums = integer.getArray();
fnums = floating.getArray();
dnums = doubles.getArray();
strings = string.getArray();
System.out.println();
System.out.println("The Values After sorting");
System.out.println();
System.out.println("Integer Values");
for (int i = 0; i < inums.length; i++)
System.out.print(inums[i] + "\t");
System.out.println();
System.out.println("Floating Values");
for (int i = 0; i < fnums.length; i++)
System.out.print(fnums[i] + "\t");
System.out.println();
System.out.println("Double Values");
for (int i = 0; i < dnums.length; i++)
System.out.print(dnums[i] + "\t");
System.out.println();
System.out.println("String Values");
for (int i = 0; i < strings.length; i++)
System.out.print(strings[i] + "\t");
}
}
$javac SortedArray.java $java SortedArray The Values Before sorting Integer Values 10 9 8 7 6 Floating Values 23.9 5.5 10.8 2.5 82.0 Double Values 12.5 244.92 1.9 98.3 35.2 String Values banana pineapple apple mango jackfruit The Values After sorting Integer Values 6 7 8 9 10 Floating Values 2.5 5.5 10.8 23.9 82.0 Double Values 1.9 12.5 35.2 98.3 244.92 String Values apple banana jackfruit mango pineapple
Related posts:
Examine the internal DNS cache
Command-Line Arguments in Java
ThreadPoolTaskExecutor corePoolSize vs. maxPoolSize
Using the Not Operator in If Conditions in Java
Deploy a Spring Boot WAR into a Tomcat Server
A Guide to the Java LinkedList
Quick Guide to @RestClientTest in Spring Boot
Java Program to Implement Double Order Traversal of a Binary Tree
Giới thiệu về Stream API trong Java 8
Get and Post Lists of Objects with RestTemplate
Guide to java.util.Formatter
Java Program to Implement the Monoalphabetic Cypher
How to Get a Name of a Method Being Executed?
Tìm hiểu về xác thực và phân quyền trong ứng dụng
Java Program to Implement Sorted Doubly Linked List
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Check if a Given Set of Three Points Lie on a Single Line or Not
Java Program to subtract two large numbers using Linked Lists
Java Program to Implement TreeSet API
Java Program to Implement Counting Sort
Hướng dẫn Java Design Pattern – Interpreter
Java Program to Solve the 0-1 Knapsack Problem
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Validate email address exists or not by Java Code
Java Program to Implement Solovay Strassen Primality Test Algorithm
Java Program to Implement PrinterStateReasons API
Java Program to Implement Sorted Circular Doubly Linked List
Receive email using POP3
Java Program to Perform Finite State Automaton based Search
Java Program to Perform Partition of an Integer in All Possible Ways
New Features in Java 14
Loại bỏ các phần tử trùng trong một ArrayList như thế nào?