(java初学者在这里) 我跟着这个Random shuffling of an array 但我想要做的是,而不是随机改变数组中的元素,然后在我的方法中使用它。我想确保更改列表的顺序是从[1] .. [2] .. [3] ..... (基本上增加) 我的最终目标是有一个数字表从洗牌阵列到增量阵列(改变)
INT [] A = {1,2,3,4,5,6},则它在我的方法
static void shuffleArray(double[] solutionArray)
{
Random rnd = new Random();
for (int i = solutionArray.length - 1; i > 0; i++)
{
int index = rnd.nextInt(i + 1);
int value = 0;
// int charValue = value.digit(0);
String next = String.valueOf((double) (value + 1));
System.out.println(next);
// Simple swap
double a = solutionArray[index];
solutionArray[index] = solutionArray[i];
solutionArray[i] = a;
}
}
public static void main(String[] args)
{
double[] solutionArray = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
double[] solution = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
shuffleArray(solutionArray);
shuffleArray(solution);
for (int i = 0; i < 7; i++)
{
position.setspeed(solutionArray[0]);
}
}
但是当我将其设置或方法使用它我希望它从1去到6,而不是随机选择一个元素,或从链接中洗牌元素。
我不明白,你能解释清楚一点吗?顺便说一下,'shuffleArray()'中的for循环永远不会终止。 – Blub
道歉误导大家, 其应该是: 对(INT I = solutionArray.length - 1; I> = 0;我 - ){} – user3035890