Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
选择排序Java_Java_Arrays_Selection Sort - Fatal编程技术网

选择排序Java

选择排序Java,java,arrays,selection-sort,Java,Arrays,Selection Sort,我试着根据硬币的价值按降序排列硬币。在我的Coin类中有一个getValue()方法。我的问题是最终结果根本没有排序。这就是我最终得到的。我似乎不知道哪里出了问题任何提示都会有帮助 在分类之前: [硬币[价值=0.25,名称=四分之一],硬币[价值=0.01,名称=便士],硬币[价值=0.1,名称=一角硬币],硬币[价值=1.0,名称=美元],硬币[价值=0.05,名称=镍]] 预期:[硬币[价值=0.25,名称=四分之一]、硬币[价值=0.01,名称=便士]、硬币[价值=0.1,名称=一角硬币

我试着根据硬币的价值按降序排列硬币。在我的Coin类中有一个getValue()方法。我的问题是最终结果根本没有排序。这就是我最终得到的。我似乎不知道哪里出了问题任何提示都会有帮助

在分类之前: [硬币[价值=0.25,名称=四分之一],硬币[价值=0.01,名称=便士],硬币[价值=0.1,名称=一角硬币],硬币[价值=1.0,名称=美元],硬币[价值=0.05,名称=镍]]

预期:[硬币[价值=0.25,名称=四分之一]、硬币[价值=0.01,名称=便士]、硬币[价值=0.1,名称=一角硬币]、硬币[价值=1.0,名称=美元]、硬币[价值=0.05,名称=镍]]

排序后: [硬币[价值=0.01,名称=便士]、硬币[价值=0.1,名称=一角硬币]、硬币[价值=0.25,名称=四分之一]、硬币[价值=1.0,名称=美元]、硬币[价值=0.05,名称=镍]]

预期:[硬币[价值=1.0,名称=美元]、硬币[价值=0.25,名称=四分之一]、硬币[价值=0.1,名称=一角硬币]、硬币[价值=0.05,名称=镍]、硬币[价值=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;
   }
}
导入java.util.array;
/**
此类使用选择排序对硬币数组进行排序
算法。
*/
公共类CoinSelectionSorter
{
//
私人硬币[]清单;
/**
构造一个选择分类器。
@param排列要排序的数组。
*/
公共硬币选择排序器(硬币[]数组)
{
列表=数组;
}
公共字符串toString()
{
返回array.toString(列表);
}
/**
查找阵列范围内最大的硬币。
@从要比较的列表中的第一个位置开始的参数
@返回硬币中最大硬币的位置
范围a[从]…a[a.长度-1]
*/
公共整数最大位置(整数从)
{
int max=从;
对于(int i=0;ilist[max].getValue()){
max=i;
}  
}
返回最大值;
}
/**
对数组排序。
*/
公共无效排序()
{
对于(int i=0;i
您可以在列表中引入数组,在对列表进行排序后,最后再次将此参数引入数组,这比使用这些参数更容易

在等级硬币中,引入公共等级硬币

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

在此方法中引入:返回this.r-prueva.r//在后续步骤中使用此选项。在后续步骤中排序

例如:

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;
    }
}
公共类硬币{
整数r;
字符串p;
公共硬币(整数r,字符串p){
//TODO自动生成的构造函数存根
这个。r=r;
这个,p=p;
}
@凌驾
公共整数比较(硬币测试){
//TODO自动生成的方法存根
返回这个.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
List fileList=Arrays.asList(List);//在列表中引入数组
集合。排序(列表);//排序表
list=filelist.toArray(list)//在数组中引入已排序的列表
很简单。。。如果您想了解我的语言,如果您不理解我的语言,我可以显示满足您需要的代码:

您使用Compariable实现的coin类可能是

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


    }
}
 ....
公共类硬币{
整数r;
字符串p;
公共硬币(整数r,字符串p){
//TODO自动生成的构造函数存根
这个。r=r;
这个,p=p;
}
@凌驾
公共整数比较(硬币测试){
//TODO自动生成的方法存根
返回这个.r-test.r;
}
}
你的类CoinSelectionSorted可能是

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


    }
}
 ....
导入java.util.array;
导入java.util.Collections;
导入java.util.List;
公共类CoinSelectionSorter{
....
公共无效排序(){
硬币[]列表=新硬币[]{新硬币(2,“约翰”),
新硬币(5,“彼得”),
新硬币(1,“罗宾”),
新硬币(15,“沃克”),
};//示例数据;
List p=Arrays.asList(List);
集合排序(p);
System.out.println(p);//只有在类中实现了ToString时,才能进行良好的打印
list=p.toArray(list);您的排序数组。
}
}
....
我希望我帮了忙


很幸运

我不知道这是否是你的问题,但我认为你写的
I
,应该是
I
。同样,在同一行,
int I=from+1
而不是
int I=0
。看看你的maximumPosition()函数。在您的评论中,您说函数应该“返回a[from]…a[a.length-1]范围内最大硬币的位置”。然而,它不是这样做的。是的,问题是使用0而不是from,使用i