Java 从列表中随机选择项目

Java 从列表中随机选择项目

列表是Java集合接口的子接口。它是一个线性结构,按顺序存储和访问每个元素。要使用列表的功能,我们使用实现了列表接口的ArrayList和LinkedList类。在本文中,我们将创建一个ArrayList,并尝试随机选择列表中的项目。

在Java中从列表中随机选择项目的程序

随机类

我们创建这个类的对象来生成伪随机数。我们将自定义这个对象并应用我们自己的逻辑来随机选择列表中的任意项目。

语法

Random nameOfObject = new Random();

示例1

以下示例说明如何使用“Random”类的对象从指定的列表中选择单个项目。

方法

  • 创建一个ArrayList,并使用’add()’方法将一些元素存储在其中。

  • 定义一个’Reandom’类的对象。

  • 对象将检查整个列表,并使用’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’的对象可能会从列表中选择两次相同的元素。这个示例演示了如何避免这种情况。

方法

在同一段代码中,我们创建一个循环,它将一直运行到‘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’的对象一起应用,以随机选择项目。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程