2015-06-21 90 views
1

我试图将jTextField的文本设置为随机选择的数组列表“sQuestions”随机部分是完整的,因为它需要与设置对应文字,我想它会像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()); 
+0

实际上你的问题是什么?请更清楚你对答案的期望。 – pnadczuk

+0

也把你的代码的随机部分。有了这个,就不可能知道问题是什么。 –

回答

0

我已经阅读4次,但我最好的猜测,你想是这样的:

Random r = new Random(); 
int randIndex = r.nextInt(sQuestions.size()); 

questions.setText(sQuestions.get(randIndex).toString()); 

如果你有一个字符串的ArrayList,我怀疑你这样做,你可以把toString()部分留出。

+0

我最初的.toString()部分被遗漏了,但它让我添加它。但我会尝试.get(randIndex) – Tristan

+0

这是因为JTextField的setText方法需要一个字符串作为输入,而不是一个ArrayList的字符串=) –

+0

非常感谢! – Tristan

0

你可以使用Random#nextInt(int)选择列表上的随机指数:

Random r = new Random(); 
int radnomIndex = r.nextInt(sQuestions.size()); 
sQuestions.get(randomIndex).setText("some text");