0

我正在尝试根据 Coin 对象的值按降序对它们的数组进行排序。我的 Coin 类中有一个 getValue() 方法。我的问题是最终结果根本没有排序。这就是我最终得到的。我似乎无法弄清楚我哪里出错了任何提示都会有所帮助

在我们排序之前:[Coin[value=0.25,name=quarter], Coin[value=0.01,name=penny], Coin[value=0.1,name=dime], Coin[value=1.0,name=dollar], Coin [值=0.05,名称=镍]]

预期:[Coin[value=0.25,name=quarter], Coin[value=0.01,name=penny], Coin[value=0.1,name=dime], Coin[value=1.0,name=dollar], Coin[value =0.05,名称=镍]]

我们排序后:[Coin[value=0.01,name=penny], Coin[value=0.1,name=dime], Coin[value=0.25,name=quarter], Coin[value=1.0,name=dollar], Coin [值=0.05,名称=镍]]

预期:[Coin[value=1.0,name=dollar], Coin[value=0.25,name=quarter], Coin[value=0.1,name=dime], Coin[value=0.05,na​​me=nickel], Coin[value =0.01,姓名=便士]]

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;
   }
}
4

1 回答 1

0

您可以在列表中引入数组,在对列表进行排序后,最后将该参数再次引入数组,这比使用这些更容易

在一个 Coin 类中,引入公共类 Coin 实现 Comparable

你应该实现一个方法:CompareTo(Coin p) // p 是一个例子

在这个方法中引入:return this.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.


    }
}
 ....

我希望我有所帮助

好运

于 2013-12-04T00:18:10.650 回答