Java 中的三元组
可能的重复:
Java 需要元组吗?
Java 是否支持三元组或至少支持对? Java支持元组吗?我正在尝试找到一种方法来制作一个列表,使其具有一个三元组,其中初始点为第一个,终点为最后,距离在中间。但是,我似乎找不到任何相关信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常只是为了这些目的创建自己的类。
I usually just create my own class for these purposes.
如果您有三个这样的相关值,这意味着您将需要一两个方法来处理它们。
如果您继续创建一个真正的类来包含这些值而不是尝试创建某种结构,您总会发现您的代码更好、更干净。
然后做所有你能做的好类的事情——将变量设为私有,在构造函数中初始化它,如果可以的话将它们设为最终的(不可变的),将直接操作数据的方法添加到类中,而不是尽可能添加吸气剂,。 ..
这通常为进一步重构和代码重用提供了很好的机会。
在这种情况下,不创建类总是很诱人,但根据我的经验,诱惑总是一些邪恶的恶魔试图向您的代码注入严重破坏。只要以正确的方式开始...
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...