Triples in Java [duplicate]

扶醉桌前 提交于 2019-12-05 17:00:22

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;
    } 
}

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...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!