我試圖從字符串問題中獲取混洗字符。但是角色重複。生成唯一的隨機數字JAVA
隨機方法
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;
}
MainActivity
strQuestion = c.getString("question");
int length = strQuestion.length();
str_buff.getChars(0, length, char_text, 0);
for(int i=0;i<length;i++){
int k = Random(length).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);
}
作爲一個數組列表將字符串轉換爲字符串並將其轉換回字符串是否更有意義... – Shashank 2015-03-02 18:15:21