📜  arraylist 中的随机项 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:23.614000             🧑  作者: Mango

代码示例1
ArrayList arraylistName = new ArrayList<>();

// add items to arraylist
arraylistName.add(item1);
arraylistName.add(item2);
arraylistName.add(item3);

Random random = new Random();
// random index between 0 and arraylistName.size();
int randomIndex = random.nextInt(arraylistName.size());

System.out.println(arraylistName.get(randomIndex));