我有一個包含 300 多個問題的 String[] 數(shù)組。當(dāng)我開始我的“測試活動”時,它會使用數(shù)組的“整個”長度(300)創(chuàng)建一個測試。但是,我只想使用該數(shù)組中的 115 個問題(隨機(jī))。這是可能的嗎?這是我的循環(huán)代碼,我認(rèn)為它負(fù)責(zé)使用的問題數(shù)量。? //This is my FOR Loop public void shuffleChapterRandomTest() { shuffledPositionChapterRandomTest = new String[chapterRandomTestQuestions.length]; for (int k = 0; k < shuffledPositionChapterRandomTest.length; k++) { shuffledPositionChapterRandomTest[k] = String.valueOf(k); } Collections.shuffle(Arrays.asList(shuffledPositionChapterRandomTest)); Log.i("TAG", "shuffle: " + shuffledPositionChapterRandomTest[0] + " " + shuffledPositionChapterRandomTest[1]);}
1 回答

MMMHUHU
TA貢獻(xiàn)1834條經(jīng)驗 獲得超8個贊
你快到了,但我認(rèn)為你的洗牌數(shù)組所做的只是保存索引的字符串值,而不是問題。如果chapterRandomTestQuestions是字符串問題的數(shù)組,則可以簡化。
Strings從數(shù)組中創(chuàng)建一個隨機(jī)列表,并返回可以根據(jù)需要迭代的混洗問題:
public List<String> shuffleChapterRandomTest() {
final List<String> randomQuestions = Arrays.asList(chapterRandomTestQuestions);
Collections.shuffle(randomQuestions);
return randomQuestions.subList(0, 115);
}
這假設(shè)列表中有超過 115 個項目,所以有點(diǎn)不安全(你可以返回randomQuestions.subList(0, Math.min(chapterRandomTestQuestions.length, 115))來阻止這個問題),否則會拋出一個IndexOutOfBoundsException
添加回答
舉報
0/150
提交
取消