如何在数组中使用get方法填充数字时对数组进行排序

时间:2013-10-19 06:50:36

标签: java arrays sorting random getter

Arrays.sort()给出错误:

FiveDice.java:19: error: no suitable method found for sort(int)
   Arrays.sort(compNums);

如果我从for循环中取出任何东西,它认为你只有1个数字或给出错误。还有哪些其他排序选项可供使用?

import java.util.*;
public class FiveDice {
    public static void main(String[] args) {
        int x;
        int compNums = 0;
        int playerNums;

        Die[] comp = new Die[5];
        Die[] player = new Die[5];
        System.out.print("The highest combination wins! \n5 of a kind, 4 of a kind, 3 of a kind, or a pair\n");
//computer
        System.out.print("Computer rolled:  ");
        for(x = 0; x < comp.length; ++x) {
            comp[x] = new Die();
            compNums = comp[x].getRoll();
           //Arrays.sort(compNums); <--does not work
           System.out.print(compNums + " ");
        }
//player
        System.out.print("\nYou rolled: \t  ");
        for(x = 0; x < player.length; ++x) {
            player[x] = new Die();
            playerNums = player[x].getRoll();
            System.out.print(playerNums + " ");
        }
    }
}

死班

public class Die {
    int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;
    public Die()
       {   } 
    public int getRoll() { 
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
        return roll; }
    public void setRoll()
        { this.roll = roll; } 

}

4 个答案:

答案 0 :(得分:1)

最简单的方法是实现Comparable to Die类,在getter方法中设置Die的构造函数中的roll值,你的问题就解决了。

public class Die implements Comparable<Die> {
    private int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;
    public Die() {
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
    }


    public int getRoll() {
         return roll;
    }        
    public void setRoll(int roll) {
         this.roll = roll;
    }

    public int compareTo(Die d) {
         return new Integer(d.getRoll()).compareTo(new Integer(this.getRoll()));
    }

}

现在Arrays.sort(Die[])将对Die数组进行排序。

答案 1 :(得分:0)

您无法在sort()上执行compNums,因为它是单个值。您将其声明为int值,而不是整数数组。

相反,您应该使compNums成为数组,使用Die滚动值填充它,然后对结果数组执行排序操作。我相信以下内容将实现您的目标。

int[] compNums = new int[5];
...
for(x = 0; x < comp.length; ++x) {
    comp[x] = new Die();
    compNums[x] = comp[x].getRoll();
}
Arrays.sort(compNums);
// print results

答案 2 :(得分:0)

您不必使用2个数组进行排序。如果实现Comparable<T>接口,则可以按Java Collections API对类进行排序。在您的情况下,Die类可以实现Comparable<T>并为Java框架提供比较骰子值的方法。

看一下Java API: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

在你的情况下:

public class Die implements Comparable<Die>{

    int roll;
    final int HIGHEST_DIE_VALUE = 6;
    final int LOWEST_DIE_VALUE = 1;

    public Die() {   } 

    public int computeRoll() { 
        roll = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
        return roll;
    }

    // I also changed this to store the value
    public int getRoll() { 
        return roll; 
    }
    public void setRoll() { this.roll = roll; }

    // This is the method you have to implement
    public int compareTo(Die d) {
        if(getRoll() < d.getRoll()) return -1;
        if(getRoll() > d.getRoll()) return +1;
        if(getRoll() == d.getRoll()) return 0;
    }

}

每当你有一个Collection Dies时,就像ArrayList或LinkedList一样,你只需对集合本身进行排序。以下是示例代码。

ArrayList<Die> myCollection = new ArrayList<Die>();
myCollection.add(die1);
// more array population code
// ...
Collections.sort(myCollection);

答案 3 :(得分:0)

你必须将数组传递给Array.Sort(arr) not参数,你必须做这样的事情Array.Sort(int[]compNums);

如果你使用像comp[]compNums这样的匿名类型,你必须这样做

java.util.Arrays.sort(comp[]compNums, new java.util.Comparator<Object[]>() {
        public int compare(Object a[], Object b[]) {
            if(something)
                return 1;

                return 0;
        }
    });