JAVA生成唯一随机数
我正在尝试从字符串问题中获取随机字符。但是这个角色是重复的 随机法JAVA生成唯一随机数,java,android,Java,Android,我正在尝试从字符串问题中获取随机字符。但是这个角色是重复的 随机法 public ArrayList<Integer> Random(int length) { ArrayList<Integer> list = new ArrayList<Integer>(); for (int i=0; i<length; i++) { list.add(new Integer(i)); }
public ArrayList<Integer> Random(int length) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i=0; i<length; i++) {
list.add(new Integer(i));
}
Collections.shuffle(list);
return list;
}
public ArrayList Random(int-length){
ArrayList=新建ArrayList();
for(int i=0;i在for循环的每次迭代中使用不同的无序排列。给定的元素通常会在不同的无序排列中出现在不同的位置,因此您可以多次看到它
改为在循环外创建一个无序列表。在for循环的每次迭代中使用不同的无序列表。给定元素通常会在不同的无序列表中出现在不同的位置,因此您可以多次看到它
改为在循环外创建一个无序列表。如果我正确理解您的问题,您希望为字符串“问题”中的每个字母创建一个新的文本视图,但您希望以随机顺序创建它们
您现在编写的内容将为MainActivity中for循环的每次迭代创建一个新的“random”ArrayList
。我认为您希望将调用移动到for循环之外的random(length)。MainActivity中的for循环应该如下所示
ArrayList<Integer> randomized = Random(length);
for(int i=0;i<length;i++){
int k = randomized.get(i);
TextView tv = new TextView(this);
tv.setText(String.valueOf(char_text[k]));
tv.setId(k);
tv.setTextSize(30);
tv.setBackgroundColor(0xff00ff00);
tv.setPadding(5, 5, 5, 5);
tv.setOnTouchListener(new MyTouchListener());
layout.addView(tv);
}
ArrayList随机化=随机(长度);
对于(int i=0;i如果我正确理解了您的问题,您希望为字符串“question”中的每个字母创建一个新的TextView,但您希望它们以随机顺序创建吗
您现在编写的内容将为MainActivity中for循环的每次迭代创建一个新的“random”ArrayList
。我认为您希望将调用移动到for循环之外的random(length)。MainActivity中的for循环应该如下所示
ArrayList<Integer> randomized = Random(length);
for(int i=0;i<length;i++){
int k = randomized.get(i);
TextView tv = new TextView(this);
tv.setText(String.valueOf(char_text[k]));
tv.setId(k);
tv.setTextSize(30);
tv.setBackgroundColor(0xff00ff00);
tv.setPadding(5, 5, 5, 5);
tv.setOnTouchListener(new MyTouchListener());
layout.addView(tv);
}
ArrayList随机化=随机(长度);
对于(int i=0;i)将字符串作为arraylist洗牌并将其转换回字符串不是更有意义吗…将字符串作为arraylist洗牌并将其转换回字符串不是更有意义吗。。。