简体   繁体   English

任何人都可以使用泛型和可比较接口为我的任务提供解决方案吗?

[英]Can anyone give me a solution to my task with Generics and Comparable interface?

I wrote half of it but cannot get it 100% correct.我写了一半,但不能 100% 正确。 Can anyone help me with it?任何人都可以帮助我吗?

Here is my problem:这是我的问题:

Write a parameterized class Triple that represents a trio of some type (eg, the Three of String, the Integer , etc.).编写一个参数化类 Triple 来表示某种类型的三重奏(例如,字符串三、整数等)。 For Triple class define:对于三重类定义:

-getElement method that receives an integer number (1,2 or 3) and returns the corresponding member. -getElement 方法接收一个整数(1,2 或 3)并返回相应的成员。 In the case of an illegal argument, throw an IllegalArgumentExpression exception -setElement that receives two arguments: an integer number (1, 2 or 3) and the value to be placed as a member of the three in the corresponding position (eg, setElement(2,3) sets 3 to y value of trio) -a constructor with 3 arguments that set the initial values of the Triple在非法参数的情况下,抛出一个 IllegalArgumentExpression 异常 -setElement,它接收两个参数:一个整数(1、2 或 3)和在相应位置作为三者中的一个成员放置的值(例如,setElement( 2,3) 将 3 设置为 trio 的 y 值) - 具有 3 个参数的构造函数,用于设置 Triple 的初始值

Then you should write the ComparableTriple class, which represents the triplet.然后你应该编写 ComparableTriple 类,它代表三元组。 Members of triplet must be comparable (implement Comparable interface).三元组的成员必须是可比较的(实现 Comparable 接口)。 Then expand the ComparableTriple class to implement the Comparable interface, so that triplets can be comparable by positions.然后扩展 ComparableTriple 类,实现 Comparable 接口,这样三元组就可以按位置进行比较了。 For example ,(2,6,1) is in the rank before (3,4,5) but behind (2,6, -4).例如,(2,6,1) 在 (3,4,5) 之前但在 (2,6, -4) 之后。 Same template can be applied to String etc.相同的模板可以应用于字符串等。

Example of main code:主要代码示例:

Triple t1 = new ComparableTriple<>(2, 6, 1);三重 t1 = 新的 ComparableTriple<>(2, 6, 1); int x = t1.compareTo(t1); int x = t1.compareTo(t1); // should be 0 // 应该是 0

//Triple class I wrote 

public class Triple<T> {
    T x, y, z;

    public Triple(T x, T y, T z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
    public T getElement(int position) {
        if (position == 1) {
            return x;
        } else if (position == 2) {
            return y;
        } else if (position == 3) {
            return z;
        } else throw new IllegalArgumentException();
    }
    public void setElement(int position, T value) {
        if (position == 1) {
            x = value;
        } else if (position == 2) {
            y = value;
        } else if (position == 3) {
            z = value;
        } else throw new IllegalArgumentException(); }
}

I tried writing ComparableTriple overriding CompareTo method but couldn't find the exact solution.我尝试编写 ComparableTriple 覆盖 CompareTo 方法,但找不到确切的解决方案。 It's not so complicated but I'm new to Generics and can't figure some things out.它并没有那么复杂,但我是泛型的新手,无法弄清楚一些事情。 Can anyone help me about the rest of code?任何人都可以帮助我了解其余的代码吗?

public class Triple<T> implements Comparable<Triple<T>> {
  ... your code here
// functional interface Comparable will force you to override the method
}

But you may want to have some kind of superclass that T extends so you can access fields for the compare.但是您可能希望拥有某种 T 扩展的超类,以便您可以访问用于比较的字段。

Example:例子:

public class Triple<T extends Number> implements Comparable<Triple<T>>

There is of course the inline compare with Collections.sort(comparable) if you don't want to commit to what T is.如果您不想承诺 T 是什么,当然可以与 Collections.sort(comparable) 进行内联比较。

That should get you started.这应该让你开始。 Post back/edit if you get stuck.如果您遇到困难,请回发/编辑。 Cheers.干杯。

(And this is not a solution by intent). (这不是有意的解决方案)。 Generics are a pain and need to be slogged through to get good at.泛型是一种痛苦,需要努力通过才能擅长。

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

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