Awt的Dimension class可以做很多事情,在不同的数字类型之间进行转换等等。我使用这些类来表示在每个ms需要更新的大2d世界上的单元格。如果我坚持使用Dimension类而不是使用自定义的Pair类,我可以获得性能优势吗?这是我的自定义Pair类:我还没有看到尺寸的任何地方提到,不知道Awt Dimension class vs自定义Pair类的性能
public class Pair<A, B> {
private A first;
private B second;
public Pair(A first, B second) {
super();
this.first = first;
this.second = second;
}
public static <A, B> Pair <A, B> createPair(A first, B second) {
return new Pair<A, B>(first, second);
}
@Override
public int hashCode() {
int hashFirst = first != null ? first.hashCode() : 0;
int hashSecond = second != null ? second.hashCode() : 0;
return (hashFirst + hashSecond) * hashSecond + hashFirst;
}
@Override
public boolean equals(Object other) {
if (other instanceof Pair) {
Pair otherPair = (Pair) other;
return
(( this.first == otherPair.first ||
(this.first != null && otherPair.first != null &&
this.first.equals(otherPair.first))) &&
( this.second == otherPair.second ||
(this.second != null && otherPair.second != null &&
this.second.equals(otherPair.second))));
}
return false;
}
@Override
public String toString()
{
return "(" + first + ", " + second + ")";
}
public A getX() {
return first;
}
public B getY() {
return second;
}
}
当我一直在寻找对实现为什么..
单元格的表示形式是一个二维数组,我只需要使用整数,维度赢呢? – 2014-09-03 11:48:42
虽然我看到我的无知,但是如果我只想用整数来定义Pair泛型,我该如何定义Pair泛型?我现在意识到我不应该有,如果我把它具体到整数,性能方面,它将与Dimension相同,对吧? – 2014-09-03 11:58:17
@RyanMarv从性能的角度来看,应该没有什么区别。如果你需要整数位置,只考虑使用'java.awt.Point'或者使用比“Pair”更富有表现力的名字:) – Thomas 2014-09-03 12:34:53