Triples in Java [duplicate]

2019-05-04 19:27发布

This question already has an answer here:

Possible Duplicate:
Does Java need tuples?

Does Java support triples or at least pairs? Does Java support tuples? I am trying to find a way to make a list so that it has a triple with initial point as first, terminal point at last, and distance in the middle. However, I can't seem to find anything about it.

2条回答
孤傲高冷的网名
2楼-- · 2019-05-04 19:50

If you have three related values like that it implies me that you will need a method or two that deal with them.

You will always find your code better and cleaner if you just go ahead and make a real class to contain these values rather than try to create some structure.

Then do all the good class stuff you can--make the variables private, initialize it in the constructor and make them final if you can (immutable), add methods that manipulate your data directly to the class rather than adding getters wherever possible, ...

This usually opens up great opportunities for further refactoring and code reuse.

It's always tempting to not create a class in cases like this but in my experience that temptation is always some evil demon trying to inject havoc into your code. Just do it the right way to start...

查看更多
叛逆
3楼-- · 2019-05-04 20:13

I usually just create my own class for these purposes.

class Pair<A,B> {
    A a;
    B b;
    public Pair( A a, B b ) {
        this.a = a;
        this.b = b;
    } 
}
查看更多
登录 后发表回答