Java Program to Implement Sorted Array

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:

Java Program to Implement Best-First Search
Java Program to Implement Merge Sort Algorithm on Linked List
Using a Custom Spring MVC’s Handler Interceptor to Manage Sessions
Hướng dẫn sử dụng String Format trong Java
Spring Cloud AWS – S3
Java Program to Implement the Program Used in grep/egrep/fgrep
Exploring the Spring Boot TestRestTemplate
Guide to the Synchronized Keyword in Java
Java Program to Implement Queue using Two Stacks
Java 8 StringJoiner
Adding a Newline Character to a String in Java
Java Program to Find the Peak Element of an Array O(n) time (Naive Method)
Java List UnsupportedOperationException
Using Custom Banners in Spring Boot
Java Program to Generate N Number of Passwords of Length M Each
Java Byte Array to InputStream
Java Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Spring Boot Gradle Plugin
HashMap trong Java hoạt động như thế nào?
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Hướng dẫn kết nối cơ sở dữ liệu với Java JDBC
Java Program to Implement RenderingHints API
Guide to the Volatile Keyword in Java
Java Program to Perform the Unique Factorization of a Given Number
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Spring Boot - Admin Server
Java Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
Java Web Services – JAX-WS – SOAP
Java Program to implement Sparse Vector
A Guide to the Java ExecutorService
Hướng dẫn Java Design Pattern – Mediator