温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

三元组在Java中的快速决策

发布时间:2024-09-16 11:56:30 来源:亿速云 阅读:82 作者:小樊 栏目:编程语言

在Java中,三元组(Triple)通常是指一个包含三个元素的数据结构

public class Triple<A, B, C> {
    public final A first;
    public final B second;
    public final C third;

    public Triple(A first, B second, C third) {
        this.first = first;
        this.second = second;
        this.third = third;
    }

    // 可选:添加 getter 方法
    public A getFirst() {
        return first;
    }

    public B getSecond() {
        return second;
    }

    public C getThird() {
        return third;
    }

    // 可选:添加 toString 方法
    @Override
    public String toString() {
        return "Triple{" +
                "first=" + first +
                ", second=" + second +
                ", third=" + third +
                '}';
    }
}

要使用这个三元组类,你可以像下面这样创建一个实例:

Triple<String, Integer, Boolean> exampleTriple = new Triple<>("Hello", 42, true);
System.out.println(exampleTriple);

输出将会是:

Triple{first=Hello, second=42, third=true}

这个简单的三元组类可以根据需要进行扩展和优化。例如,你可以为其添加更多的方法,或者让它实现某些接口。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI