所以我应该建立这样一个置换列表这个数组列表:排列如何打印10所列出
输出:
,我似乎无法让我的代码重复10次(编辑:它告诉我, numbers.add(og.get(randPick));` 是出界,因为它变成了负数
CODE:
import java.util.ArrayList;
import java.util.Random;
public class ArrayListTest
{
public void Arrays(){
ArrayList<Integer> og = new ArrayList<Integer>();
Random random = new Random();
//what is inside the Array (Digits 1-10)
for (int x = 1; x <= 10; x++) {
og.add(x);
}
ArrayList<Integer> numbers = new ArrayList<Integer>();
//printing
int a = 1;
while(a < 10){
System.out.print("List " + a + ":");
a++;
for(int y = 0; y < 10; y++){
//the random number itself
int randPick = random.nextInt(og.size());
//adding the new random number to the permutation
numbers.add(og.get(randPick));
//taking out the random number slot chosen
og.remove(randPick);
//printing out the current numbers in one line
System.out.print(numbers.get(y) + " ");
}
}
}
}
到底是什么问题? – Idos
@bmarkham,这个描述在编码方面没有用处。 OP应该添加一个确切的描述发生什么,这是一个例外?有什么打印?什么打印? – amotzg
你的问题在于'og.remove(randPick)'。这将列表大小从10更改为9,因此在while循环的下一次迭代中,“randomPick”(索引)可能为9,而列表大小仅为8,导致例外。另外,正如其他人所说的,你应该添加问题所在(包括堆栈跟踪),而不要让其他人为自己运行代码。 ''我似乎无法让我的代码重复10次“。好吧,但为什么? *特定*问题是什么? (在这种情况下要回答这个问题:异常+堆栈跟踪)。 –