【发布时间】:2019-01-15 13:07:43
【问题描述】:
提前感谢您的宝贵时间! 我正在研究 Fruchterman Reingold Graph 创建器,但我陷入了一个看似非常简单的问题。长话短说,我想初始化一个 Hashmap,其中包含一个整数键,即我所在的顶点,以及一个整数列表,即 x 位置和 y 位置。然而,下面的代码将每个键放置 8 个而不是 2 个位置值而不是 2 个。非常感谢任何帮助或提示!
package testing.ground;
import java.util.*;
public class TestingGround {
public static void main(String[] args) {
Random ranx = new Random();
Random rany = new Random();
HashMap<Integer,List<Integer>> positions=new HashMap<Integer,List<Integer>>();
List<Integer> temp = new ArrayList<Integer>();
int n = 3;
int area = 1280*720;
int vposx=0;
int vposy=0;
double k = 0.5*(Math.sqrt(area/n));
for (int i=0; i<=n; i++){
vposx=ranx.nextInt(1280)+1;
vposy=rany.nextInt(720)+1;
temp.add(vposx);
temp.add(vposy);
positions.put(i,temp);
}
System.out.println(positions);
}
}
结果是这些
{0=[1063, 102, 41, 391, 614, 418, 751, 599], 1=[1063, 102, 41, 391, 614, 418, 751, 599], 2=[1063, 102 , 41, 391, 614, 418, 751, 599], 3=[1063, 102, 41, 391, 614, 418, 751, 599]}
预期的只是0=[randomx,randomy], 2=[randomx,randomy] and so on。
【问题讨论】: