/*
* Program memakai metode compareTo
*/
package metode_compare_to;
/**
* @author fachrul
*/
public class Main {
public static void main(String[] args) {
String[] Temanku={"Toni","Zaki","Aldo","Badu"};
String smt;
System.out.println("Sebelum di urut=");
for(int i=0;i<Temanku.length;i++)
System.out.println(Temanku[i]);
//proses bubble sort
System.out.println("Sesudah di urut=");
for(int i=0;i<Temanku.length-1;i++)
{
for(int j=0;j<Temanku.length-1;j++)
{
if(Temanku[j].compareTo(Temanku[j+1])>0)
{
smt=Temanku[j+1];
Temanku [j+1] =Temanku[j];
Temanku[j]=smt;
}
}
}
for(int i=0;i<Temanku.length;i++)
{
System.out.println(Temanku[i]);
}
}
}
* Program memakai metode compareTo
*/
package metode_compare_to;
/**
* @author fachrul
*/
public class Main {
public static void main(String[] args) {
String[] Temanku={"Toni","Zaki","Aldo","Badu"};
String smt;
System.out.println("Sebelum di urut=");
for(int i=0;i<Temanku.length;i++)
System.out.println(Temanku[i]);
//proses bubble sort
System.out.println("Sesudah di urut=");
for(int i=0;i<Temanku.length-1;i++)
{
for(int j=0;j<Temanku.length-1;j++)
{
if(Temanku[j].compareTo(Temanku[j+1])>0)
{
smt=Temanku[j+1];
Temanku [j+1] =Temanku[j];
Temanku[j]=smt;
}
}
}
for(int i=0;i<Temanku.length;i++)
{
System.out.println(Temanku[i]);
}
}
}
Komentar
Posting Komentar