我正在学习Java,而且我遇到了ArrayList的问题或者检索了一行随机文本。我想要实现的是随机引用(从列表中)加载每个页面刷新。从来没有做过数组/随机,不确定这是否是正确的方法?
到目前为止工作的是https://jsfiddle.net/5wryh23L/
代码无效:
Random random = new Random();
int index = random.nextInt(10);
List<String> list = new ArrayList<String>();
list.add("quote1");
list.add("quote2");
list.add("quote3");
list.add("quote4");
list.add("quote5");
RandomExample obj = new RandomExample();
for(int i = 0; i < 10; i++){
System.out.println(obj.getRandomList(list));
}
}
getRandomList(List<String> list) {
//0-4
int index = random.nextInt(list.size());
System.out.println("\nIndex :" + index );
return list.get(index);
}
答案 0 :(得分:2)
从列表中获取随机项目的一种非常简单的方法:
list.get((int)(Math.random()*list.size()));
答案 1 :(得分:0)
您的随机方法显然是正确的,也是您构建列表的方式。我相信你的问题是因为你没有主要方法吗?如果您在 main方法中创建列表并在main方法中调用 getRandomList(),它将起作用。它或多或少会像这样:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
static Random random = new Random();
int index = random.nextInt(10);
public static void main(String args[]){
List<String> list = new ArrayList<String>();
list.add("quote1");
list.add("quote2");
list.add("quote3");
list.add("quote4");
list.add("quote5");
// RandomExample obj = new RandomExample
for(int i = 0; i < 10; i++){
System.out.println(getRandomList(list));
}
}
static String getRandomList(List<String> list) {
//0-4
int index = random.nextInt(list.size());
System.out.println("Index :" + index );
return list.get(index);
}
答案 2 :(得分:0)
This example gives how to shuffle elements in the ArrayList. By calling Collections.shuffle() method you can shuffle the content of the ArrayList. Every time you call shuffle() method, it generates different order of output.
**Example #1**
public class MyListShuffle {
public static void main(String a[]){
ArrayList<String> wordList = new ArrayList<String>();
list.add("Stack");
list.add("Overflow");
list.add("is");
list.add("a");
list.add("place");
list.add("great");
list.add("to";
list.add("learn");
list.add("and");
list.add("teach");
Collections.shuffle(wordList);
System.out.println("Results after shuffle operation #1:");
for(String word: wordList){
System.out.println(word);
}
Collections.shuffle(wordList);
System.out.println("Results after shuffle operation #2:");
for(String word: wordList){
System.out.println(word);
}
}
}
Note that The shuffle(List<?>) method is used to randomly permute the specified list using a default source of randomness.
The original declaration:
- java.util.Collections.shuffle() method.
- public static void shuffle(List<?> list)
- Parameter: "list" is the list to be shuffled.
Output:
Results after shuffle operation #1:
to
learn
and
Stack
Overflow
a
teach
is
great
place
Results after shuffle operation #2:
Overflow
place
great
and
teach
to
is
learn
Stack
a
示例#2
public class ShuffleList {
public static void main(String args[]) {
// create an array list object
List myList = new ArrayList();
// populate the list
myList.add("A");
myList.add("B");
myList.add("C");
myList.add("D");
myList.add("E");
System.out.println("Collection after: "+ myList);
// shuffle the list
Collections.shuffle(myList);
System.out.println("Collection after shuffle: "+ myList);
}
}
After compiling the program, the output is:
Collection before shuffle: [A, B, C, D, E]
Collection after shuffle: [C, A, E, B, D]
或者您可以按以下方式返回
Example #3
return list.get(random.nextInt(list.size()));