Wednesday, 29 November 2017

Java Program to Implement Sorted Array


Code:

import java.util.Arrays;

public class SortedArray
{
    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 = new SortedArray(inums);
        SortedArray floating = new SortedArray(fnums);
        SortedArray doubles = new SortedArray(dnums);
        SortedArray string = new SortedArray(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");
    }
}


Output:

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



More Java Programs:





















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...