[英]What could be a more efficient replacement of this data structure?
我有一个名为Pair的类,该类可容纳成对的整数值。
public class Pair implements Comparable<Pair>
{
public int word1;//1st integer of the pair
public int word2;//2nd integer of the pair
public Pair(int w1, int w2)
{
word1 = w1;
word2 = w2;
}
@Override
public int compareTo(Pair input)
{
if (input.word1 == word1)
{
if (input.word2 == word2)
return 0;
else if (input.word2 < word2)
return 1;
else
return -1;
}
else if (input.word1 == word2)
{
if (input.word2 == word1)
return 0;
else if (input.word2 < word1)
return 1;
else
return -1;
}
else if (input.word1 > word1)
return -1;
return 1;
}
}
作为存储整数对而不是此类的更好和有效的方法,我可以使用什么? 我可以使用数组列表吗? 会不会更有效率?
您似乎试图实现的一种典型的实现词典比较的模式是:
int r;
r = Integer.compare(word1, input.word1);
if(r==0) return r;
return Integer.compare(word2, input.word2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.