[英]Trouble sorting an array of objects
問題如下:編寫一個名為SortSalon的程序,該程序包含一個數組,用於容納六個HairSalon對象,並用數據填充它。 包括一種按服務價格升序對數組進行排序的方法。 調用該方法並顯示結果。
public class SortSalon
{
private static HairSalon [] itsArray = new HairSalon[6];
public SortSalon()
{
itsArray[0] = new HairSalon("cut", 10.50, 15);
itsArray[1] = new HairSalon("shampoo", 5.00, 10);
itsArray[2] = new HairSalon("manicure", 20.00, 20);
itsArray[3] = new HairSalon("cut", 10.50, 15);
itsArray[4] = new HairSalon("manicure", 20.00, 20);
itsArray[5] = new HairSalon("manicure", 20.00, 20);
}
public HairSalon [] sortByPrice(HairSalon [] par)
{
HairSalon [] newArray = new HairSalon[6];
int x = 0;
int y = 0;
HairSalon smallest = itsArray[1];
for(int i = 0; i < itsArray.length; i++)
{
while(y < itsArray.length - 1)
{
if(itsArray[y].getPrice() == smallest.getPrice())
{
smallest = itsArray[y];
newArray[x] = smallest;
}
else
{
//smallest = itsArray[y];
for(int c = 0; c < itsArray.length - 1; c++)
{
if(itsArray[y].getPrice() < itsArray[y + 1].getPrice()
&& itsArray[y].getPrice() > smallest.getPrice())
{
smallest = itsArray[y];
newArray[x] = smallest;
}
}
}
y++;
}
y = 0;
//newArray[x] = smallest;
x++;
}
int z = 0;
System.out.println("Ascending order: ");
while(z < newArray.length)
{
System.out.println(newArray[z].toString());
z++;
}
return newArray;
}
public static void main()
{
SortSalon test = new SortSalon();
test.sortByPrice(itsArray);
}
}
無法獲得按價格正確排序對象的方法。 任何建議將不勝感激!
使用Comparator
對HairSalon對象進行排序,您可以定義自定義比較方式,並使用Arrays.sort(T[] a, Comparator<? super T> c)
進行排序
您可以選擇Comparable
或Comparator
進行排序。 我更喜歡使用Comparator
,原因是比較邏輯在單獨的類中。 這意味着您無需修改HairSalon
類,並且可以具有多個自定義的Comparators類,用於不同的字段比較/排序。
樣品:
public class HairSalon {
private final String type;
private final double price;
public HairSalon(String type, double price){
this.type = type;
this.price = price;
}
public String getType(){ return this.type;}
public double getPrice(){return this.price;}
}
public class PriceComparator implements Comparator<HairSalon> {
/**
*comparison/sorting logic is here
*/
@Override
public int compare(HairSalon hs1, HairSalon hs2)
{
if(hs1.getPrice() > hs2.getPrice()){return 1;}
else if(hs1.getPrice() == hs2.getPrice()){return 0;}
else{return -1;}
}
}
您如何對其進行排序:
//if you put all your HairSalon objects in an array:
Arrays.sort(hairSalonsArray, new PriceComparator())
//if you put all your HairSalon in a list
Collections.sort(hairSalonsList, new PriceComparator());
我認為最好的方法(使用已經定義的排序算法是使用接口“ Comparable”,例如:
private class HairSalon implements Comparable<HairSalon>{
public String type = null;
public double price = 0;
public float number = 0;
public HairSalon(String type,double price,float number){
this.type = type;
this.price = price;
this.number = number;
}
@Override
public int compareTo(HairSalon compa) {
int ret = 0;
if(this.price < compa.price) ret = -1;
if(this.price > compa.price){
ret = 1;
}else{ret = 0;}
return ret;
}
}
然后,您可以使用Collections排序算法:
Collections.sort(new ArrayList<HairSalon>(Arrays.asList(itsArray)));
但是,您在使用任何排序機制的特定實現時都遇到了麻煩,這無濟於事。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.