Generics and Arrays - Sorting
// Generics and arrays
// Sorting
class Generics<T extends Comparable<T>>
{
T []x;
public Generics(T []Arr)
{
x = Arr;
}
void fnDisplay()
{
System.out.print("\nArray elements : ");
for(int i = 0; i<x.length;i++)
System.out.print(x[i]+" ");
}
void fnSort()
{
T Tmp;
for(int i = 0;i<x.length-1;i++)
for(int j = i+1; j<x.length;j++)
{
if(x[i].compareTo(x[j])>0)
{
Tmp = x[i];
x[i] = x[j];
x[j] = Tmp;
}
}
}
}
class Generics_Demo
{
public static void main(String as[])
{
Integer []A1 = {1,3,5,4,2};
Generics<Integer> obj1 = new Generics<Integer>(A1);
obj1.fnDisplay();
obj1.fnSort();
obj1.fnDisplay();
String []A2 = {"IT", "CSE", "ECE", "AUTO"};
Generics<String> obj2 = new Generics<String>(A2);
obj2.fnDisplay();
obj2.fnSort();
obj2.fnDisplay();
Double []A3 = {7.3, 1.2,4.2,3.6,0.8};
Generics<Double> obj3 = new Generics<Double>(A3);
obj3.fnDisplay();
obj3.fnSort();
obj3.fnDisplay();
}
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu