简体   繁体   English

乘法表

[英]Multiplication table

I'm making multiplication table(from 2 to 9) - that is 10 randomly generated samples, for example 我正在制作乘法表(从2到9)-例如,这是10个随机生成的样本

2 * 3 = 
4 * 5 = 
... (7 more times)
9 * 5 = 

The point is all samples must be different and samples 关键是所有样本必须与样本不同

5 * 8 = 

and

8 * 5 = 

considered to be the same 被认为是相同的

My idea is to make class Pair that describes pair of numbers to be multiplied, override it's equals method, generate random numbers, create Pair and add Pair objects to Set. 我的想法是使Pair类描述要被相乘的数字对,重写其equals方法,生成随机数,创建Pair并将Pair对象添加到Set。

public class Pair {
    private int first;
    private int second;

    public int getFirst() {
        return first;
    }

    public int getSecond() {
        return second;
    }

    public void setFirst(int first) {
        this.first = first;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    public Pair() {}

    public Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public boolean equals(Object o) {
        if (o == null || o.getClass() != this.getClass())
            return false;

        Pair that = (Pair) o;
        return (this.first == that.first && this.second == that.second) ||
                (this.second == that.first && this.first == that.second);
    }

    @Override
    public int hashCode() {
        int result = 17;
        int prime = 31;
        result = result * prime + first;
        result = result * prime + second;
        return result;
    }

    @Override
    public String toString() {
        return first + " * " + second + " =";
    }
}

public static Pair[] createTable(int count) {
        Random random = new Random();
        Set<Pair> set = new HashSet<Pair>();
        while (set.size() < count) {
            int first = random.nextInt(8) + 2;
            int second = random.nextInt(8) + 2;
            set.add(new Pair(first, second));
        }
        return set.toArray(new Pair[count]);
}

The problem is that some arrays that method createTable() returns consist equivalent pairs for example in 问题是方法createTable()返回的某些数组包含等效对,例如

[7 * 6 =, 5 * 6 =, 4 * 8 =, 4 * 9 =, 2 * 8 =, 9 * 2 =, 8 * 2 =, 6 * 3 =, 5 * 2 =, 4 * 2 =]

there are pairs 2 * 8 and 8 * 2 not supposed to be there 有对2 * 8和8 * 2不应该在那里

where is the mistake? 错误在哪里?

Your hashCode() method is wrong. 您的hashCode()方法错误。 hashCode() MUST return equal values for two objects that are equal. hashCode() 必须为两个相等的对象返回相等的值。 The pair 5-7 is equal to the pair 7-5 according to your equals() method, but their hashCode() is not the same. 根据您的equals()方法,对5-7等于对7-5,但是它们的hashCode()不相同。

To implement hashCode() correctly, you should always start by the lowest number: 为了正确实现hashCode() ,您应该始终以最低的数字开头:

public int hashCode() {
    int result = 17;
    int prime = 31;

    int lowest = first < second ? first : second;
    int highest = first < second ? second : first;

    result = result * prime + lowest;
    result = result * prime + highest;
    return result;
}

or, simpler: 或者,更简单:

public int hashCode() {
    int lowest = first < second ? first : second;
    int highest = first < second ? second : first;
    return Objects.hash(lowest, highest);
}

or, even simpler (thanks @user2336315): 或者,甚至更简单(感谢@ user2336315):

public int hashCode() {
    return Objects.hash(Math.min(first, second), Math.max(first, second));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM