我试图根据硬币对象的值按降序排列它们的数组。在我的硬币类中有一个getValue()方法。我的问题是,最终结果根本没有排序。这就是我最后得到的。我似乎不知道我哪里出了差错,任何建议都会有帮助
排序前:[Coinvalue=0.25,name=quarter,Coinvalue=0.01,name=penny,Coinvalue=0.1,name=dime,Coinvalue=1.0,name=dollar,Coinvalue=0.05,name=nickel]
预期:[Coinvalue=0.25,name=quarter,Coinvalue=0.01,name=penny,Coinvalue=0.1,name=dime,Coinvalue=1.0,name=dollar,Coinvalue=0.05,name=nickel]
排序后:[Coinvalue=0.01,name=penny,Coinvalue=0.1,name=dime,Coinvalue=0.25,name=quarter,Coinvalue=1.0,name=dollar,Coinvalue=0.05,name=nickel]
预期:[Coinvalue=1.0,name=dollar,Coinvalue=0.25,name=quarter,Coinvalue=0.1,name=dime,Coinvalue=0.05,name=nickel,Coinvalue=0.01,name=penny]
import java.util.Arrays;
/**
This class sorts an array of coins, using the selection sort
algorithm.
*/
public class CoinSelectionSorter
{
//
private Coin[] list;
/**
Constructs a selection sorter.
@param anArray the array to sort.
*/
public CoinSelectionSorter(Coin[] anArray)
{
list = anArray;
}
public String toString()
{
return Arrays.toString(list);
}
/**
Finds the largest coin in an array range.
@param from the first position in a to compare
@return the position of the largest coin in the
range a[from] . . . a[a.length - 1]
*/
public int maximumPosition(int from)
{
int max = from;
for(int i = 0; i < list.length-1; i++){
if(list[i].getValue() > list[max].getValue()){
max = i;
}
}
return max;
}
/**
Sorts an array.
*/
public void sort()
{
for(int i = 0; i < list.length -1; i++){
int max = maximumPosition(i);
swap(i, max);
}
}
/**
Swaps two entries of the array.
@param i the first position to swap
@param j the second position to swap
*/
public void swap(int i, int j)
{
Coin temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}发布于 2013-12-04 00:18:10
您可以在列表中引入数组,在对列表进行排序并最终将这些参数再次引入数组之后,使用这些参数比使用这些参数更容易。
在类硬币中,介绍公共类硬币实现可比较的
您应该实现一个方法:CompareTo(Coin p) // p就是一个例子
在这个方法中介绍:返回这个.r-prueva.r; //您在接下来的步骤中使用它作为下一步的Comparable.Sort
示例:
public class Coin implements Comparable<Coin >{
Integer r;
String p;
public Coin(Integer r,String p) {
// TODO Auto-generated constructor stub
this.r = r;
this.p = p;
}
@Override
public int compareTo(Coin test) {
// TODO Auto-generated method stub
return this.r - test.r;
}
}那没问题..。现在,在您的类中,您可以创建一个方法或引入这个方法来对数组进行排序:
List<Coin> fileList = Arrays.asList(list); // Introduce Array in List
Collections.sort(list); // sort List
list = filelist.toArray(list) // introduce the sorted list in array很容易..。如果您需要,如果不理解我的语言,我可以向您展示这段代码,以满足您的需要:
你的硬币类可与之媲美.
public class Coin implements Comparable<Coin >{
Integer r;
String p;
public Coin(Integer r,String p) {
// TODO Auto-generated constructor stub
this.r = r;
this.p = p;
}
@Override
public int compareTo(Coin test) {
// TODO Auto-generated method stub
return this.r - test.r;
}
}你的CoinSelectionSorted班可能.
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CoinSelectionSorter{
....
public void sort() {
Coin[] list = new Coin[] {new Coin(2, "Johan"),
new Coin(5, "peter"),
new Coin(1, "robin"),
new Coin(15, "walker"),
}; // example data;
List <Coin> p = Arrays.asList(list);
Collections.sort(p);
System.out.println(p); // only good print if you have implement ToString in class coin
list = p.toArray(list); your sorted array.
}
}
....我希望我能帮上忙
好幸运
https://stackoverflow.com/questions/19508159
复制相似问题