【发布时间】:2016-05-20 16:58:25
【问题描述】:
当我在测试两个明显相似的代码时遇到问题时,我试图用随机向量 2 填充数组“星”。
头
Array<Vector2> stars;
int numStars;
第一个代码
public void initStars() {
int a = 100;
int b = 120;
numStars = (int) (a * b * 0.01f);
stars = new Array<Vector2>(numStars);
Random rand = new Random();
Vector2 star = new Vector2();
for (int i = 0 ;i <numStars ;i++){
star.set(rand.nextInt(a),rand.nextInt(b));
stars.add(star);
}
}
第二个代码
public void initStars() {
int a = 100;
int b = 120;
numStars = (int) (a * b * 0.01f);
stars = new Array<Vector2>(numStars);
Random rand = new Random();
for (int i = 0 ;i <numStars ;i++){
Vector2 star = new Vector2();
star.set(rand.nextInt(a),rand.nextInt(b));
stars.add(star);
}
}
第一个不起作用,只需用循环中生成的最后一个随机向量填充数组,即。数组中的所有向量都是相等的,第二个工作得很好,我的问题是,如果两个代码显然是等价的,为什么会发生这种情况。
【问题讨论】: