在Java中随机选择列表中的项目
List是Java集合接口的子接口。它是一种线性结构,以顺序方式存储和访问每个元素。为了使用列表的功能,我们使用实现列表接口的ArrayList和LinkedList类。在本文中,我们将创建一个ArrayList并尝试随机选择该列表中的项目。
Java中随机选择列表中项目的程序
Random类
我们创建此类的对象以生成伪随机数。我们将自定义此对象并应用我们自己的逻辑来从列表中随机选择任何项目。
语法
Random nameOfObject = new Random();
示例1
以下示例说明了如何使用“Random”类的对象从指定的列表中选择单个项目。
方法
创建一个ArrayList,并使用“add()”方法在其中存储一些元素。
定义“Random”类的对象。
该对象将检查整个列表,并使用“nextInt()”方法选择一个项目。然后,使用“get()”方法,我们将获取该项目并将其存储在一个整数变量中。
示例
import java.util.*;
public class Randomly {
public static void main(String[] args) {
// Creating arraylist
ArrayList<Integer> araylist = new ArrayList<Integer>();
// Adding elements in arraylist
araylist.add(8);
araylist.add(5);
araylist.add(2);
araylist.add(9);
araylist.add(4);
araylist.add(7);
System.out.println("Elements of the list : ");
// loop to iterate through elements
for(int i = 0; i < araylist.size(); i++ ) {
// to print the elements in the list
System.out.println(araylist.get(i));
}
Random rndm = new Random();
// creating object
int rndmElem = araylist.get(rndm.nextInt(araylist.size()));
System.out.println("Selecting a random element from the list : " + rndmElem);
}
}
输出
Elements of the list : 8 5 2 9 4 7 Selecting a random element from the list : 8
示例2
“Random”类的对象可能会从列表中重复选择一个元素两次。此示例演示了如何从列表中选择多个项目。
方法
创建一个ArrayList,并使用“add()”方法在其中存储一些元素。
定义“Random”类的对象。
现在,声明一个名为“noOfrndmElem”的整数变量,该变量将存储要选择的项目数。
创建一个for循环,该循环将运行到“noOfrndmElem”并选择项目。
import java.util.*;
public class Randomly {
public static void main(String[] args) {
// Creating arraylist
ArrayList<Integer> araylist = new ArrayList<Integer>();
// Adding elements in arraylist
araylist.add(8);
araylist.add(5);
araylist.add(2);
araylist.add(9);
araylist.add(4);
araylist.add(7);
System.out.println("Elements of the list : ");
// loop to iterate through elements
for(int i = 0; i < araylist.size(); i++ ) {
// to print the elements in the list
System.out.println(araylist.get(i));
}
Random rndm = new Random();
int noOfrndmElem = 2;
System.out.println("Selecting two elements randomly from the list : ");
for (int i = 0; i < noOfrndmElem; i++) {
int rndmIndx = rndm.nextInt(araylist.size());
Integer rndmElem = araylist.get(rndmIndx);
System.out.print(rndmElem + " ");
}
}
}
输出
Elements of the list : 8 5 2 9 4 7 Selecting two elements randomly from the list : 8 2
示例3
前面我们讨论过“Random”类的对象可能会从列表中重复选择一个元素两次。此示例演示了如何避免这种情况。
方法
在同一代码中,我们创建了一个for循环,该循环将运行到“noOfrndmElem”并选择项目。选择后,它将使用内置方法“remove()”从列表中删除该元素。我们通过索引访问和删除元素。
import java.util.*;
public class Randomly {
public static void main(String[] args) {
// Creating arraylist
ArrayList<Integer> araylist = new ArrayList<Integer>();
// Adding elements in arraylist
araylist.add(8);
araylist.add(5);
araylist.add(2);
araylist.add(9);
araylist.add(4);
araylist.add(7);
System.out.println("Elements of the list : ");
// loop to iterate through elements
for(int i = 0; i < araylist.size(); i++ ) {
// to print the elements in the list
System.out.println(araylist.get(i));
}
Random rndm = new Random();
int noOfrndmElem = 2;
System.out.println("Selecting two elements randomly from the list : ");
for (int i = 0; i < noOfrndmElem; i++) {
int rndmIndx = rndm.nextInt(araylist.size());
Integer rndmElem = araylist.get(rndmIndx);
System.out.print(rndmElem + " ");
araylist.remove(rndmIndx);
}
}
}
输出
Elements of the list : 8 5 2 9 4 7 Selecting two elements randomly from the list : 7 2
结论
在本文中,我们讨论了一些Java程序,用于随机选择列表中的项目。我们从定义列表开始,然后定义一个名为“Random”的类,该类用于生成随机数。我们定义了一个自定义逻辑,并将其与“Random”类的对象一起应用,以随机选择项目。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP