首页  >  文章  >  Java  >  在Java中从列表中随机选择项目

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

王林
王林转载
2023-09-06 20:33:111409浏览

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

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

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

随机类别

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

语法

Random nameOfObject = new Random();

Example 1

的翻译为:

示例1

下面的示例说明了如何使用‘Random’类的对象从指定的列表中选择单个项目。

方法

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

  • 定义一个‘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”类的对象可以从列表中选择一个元素两次。此示例演示了如何从列表中选择多个项目。

方法

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

  • 定义一个‘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 

Example 3

的中文翻译为:

示例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中从列表中随机选择项目的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除