我有一个点坐标列表,想要迭代一个 2D 列表,然后处理哈希函数中的元素以生成哈希表的 key 。 我有点难以迭代 List> 点,还有如何将坐标 (x,y) 作为值传递给哈希表(键,值)?
public static List<List<Integer>> myMethod(int k, List<List<Integer>> points){
Hashtable pt = new Hashtable();
for (int i = 0; i <points.size(); i++)
{
for (int j = 0; j < points.get(i).size(); j++)
{
Integer x = points.get(i);
Integer y = points.get(j);
pt.put(hashfunction( x, y), points.get(i));
}
}
//return list of pairs ;
}
最佳答案
for (int i = 0; i <points.size(); i++) {
List<Integer> in = points.get(i);
for (int j = 0; j < in.size() - 1; j++) {
Integer x = in.get(j);
Integer y = in.get(j + 1);
pt.put(hashfunction(x, y), points.get(i));
}
}
关于java - 迭代二维列表并将元素放入哈希表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53045336/