[英]Setting text to specific part of array list
我正在嘗試將jTextField的文本設置為Array列表“ sQuestions”的隨機選擇的項目,隨機部分是完整的,因為需要使其與設置的文本相對應,我想這就像questions.setText(sQuestions[n])
一樣questions.setText(sQuestions[n])
但隨后我不得不添加.toString()
部分,現在我不知道如何調用sQuestions
的特定部分,而不是出現整個數組。
Random randNum = new Random();
if("science".equals(Choice)){
int n = randNum.nextInt(sAnswers.size());
}
ArrayList<String> sQuestions = new ArrayList<>();
questions.setText(sQuestions.toString());
您可以使用Random#nextInt(int)
在列表上選擇一個隨機索引:
Random r = new Random();
int radnomIndex = r.nextInt(sQuestions.size());
sQuestions.get(randomIndex).setText("some text");
我已經閱讀了4次,但據我最大的猜測,您想要這樣的東西:
Random r = new Random();
int randIndex = r.nextInt(sQuestions.size());
questions.setText(sQuestions.get(randIndex).toString());
如果您懷疑有一個ArrayList of Strings,則可以省略toString()
部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.