📌  相关文章
📜  如何从Java中的向量中获取随机元素?

📅  最后修改于: 2022-05-13 01:55:15.733000             🧑  作者: Mango

如何从Java中的向量中获取随机元素?

Java 中的Vector是Java集合框架的一部分。 Vector 是一个动态的对象数组,即可以根据需要修改向量的大小。 Vector 实现了List接口。它还维护插入顺序,并且可以使用它们的索引访问向量的元素。 Java中的向量是同步的。

我们可以通过以下方式访问向量元素:

  • 使用他们的索引
  • 通过使用迭代器
  • 通过从向量中随机调用元素

从向量中获取随机元素的不同方法:

  1. 使用 Math 类的 random() 方法
  2. 使用 Random 类
  3. 使用 ThreadLocalRandom 类

方法一:使用Math类的random()方法

Java.lang包的Math类有一个random()方法,它返回一个大于 0.0 且小于 1.0 的正双精度值。我们可以使用此方法生成随机索引并访问给定向量中该索引处的元素。

Java
// Java program to access random elements from the vector
// using random() method of Math class
  
import java.util.Vector;
public class GfG {
    
    // Create an instance of type vector which accepts
    // elements of String type
    static Vector vector;
  
    // getRandomElements() method which accesses random
    // elements from the vector
    static void getRandomElements()
    {
        // Run a loop as many times as the number of
        // elements you want to access
        for (int i = 0; i < vector.size(); i++) 
        {
            // Generate a random int value between 0 and the
            // last index of the vector
            int index = (int)(Math.random() * vector.size());
            
            // get the element at the generated random index
            System.out.println(vector.get(index));
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        // Instantiate the vector instance
        vector = new Vector();
        
        // Add elements into the vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("For");
        vector.add("Geeks");
        
        // Call the getElements() method on this vector
        // object
        getRandomElements();
    }
}


Java
// Java program to access random elements from the vector
// using random class
  
import java.util.Random;
import java.util.Vector;
class GFG {
    
    // Create a Vector instance of type String
    static Vector vector;
    
    // getRandomElements() method which accesses the
    // random elements from the given vector
    static void getRandomElements()
    {
        // create new object of the Random class
        Random random = new Random();
        
        // Run a loop as many times as the number of
        // elements you want to access
        for (int i = 0; i < vector.size(); i++)
        {
            // call the nextInt() method of this random
            // object to generate a random int value
            int index = random.nextInt(vector.size());
            
            // Get the element present at this random index
            // in the given vector
            System.out.println(vector.get(index));
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        // Instantiate the vector object
        vector = new Vector();
        
        // Add elements into the Vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("For");
        vector.add("Geeks");
        
        // Call the getRandomElements() method on this
        // vector object
        getRandomElements();
    }
}


Java
// Java program to access random elements from the given
// vector using ThreadLocalRandom class
  
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
class GFG {
    
    // Create a Vector instance which accepts elements of
    // String type
    static Vector vector;
    
    // getRandomElements() method which accesses random
    // elements from the given vector
    static void getRandomElements()
    {
        // Run a loop as many times as the number of
        // elements you want to access
        for (int i = 0; i < vector.size(); i++) 
        {
            // Generate a random integer by calling the
            // ThreadLocalRandom.current().nextInt() method
            int index = ThreadLocalRandom.current().nextInt(vector.size());
            
            // Print the element present at this random
            // index in the given vector
            System.out.println(vector.get(index));
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        vector = new Vector();
        
        // Add elements into the vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("For");
        vector.add("Geeks");
        
        // Call the getRandomElements() method on this
        // vector object
        getRandomElements();
    }
}


输出
For
To
Welcome
Welcome
Geeks

方法 2:使用 Random 类

为了生成随机索引,我们还可以使用Java.util包的Random类。它提供了有用的方法来生成指定类型和指定范围内的随机数。

Java

// Java program to access random elements from the vector
// using random class
  
import java.util.Random;
import java.util.Vector;
class GFG {
    
    // Create a Vector instance of type String
    static Vector vector;
    
    // getRandomElements() method which accesses the
    // random elements from the given vector
    static void getRandomElements()
    {
        // create new object of the Random class
        Random random = new Random();
        
        // Run a loop as many times as the number of
        // elements you want to access
        for (int i = 0; i < vector.size(); i++)
        {
            // call the nextInt() method of this random
            // object to generate a random int value
            int index = random.nextInt(vector.size());
            
            // Get the element present at this random index
            // in the given vector
            System.out.println(vector.get(index));
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        // Instantiate the vector object
        vector = new Vector();
        
        // Add elements into the Vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("For");
        vector.add("Geeks");
        
        // Call the getRandomElements() method on this
        // vector object
        getRandomElements();
    }
}
输出
Welcome
Geeks
To
Geeks
For

方法 3:使用 ThreadLocalRandom 类

Java.util.concurrentThreadLocalRandom类是一个与当前线程隔离的随机数生成器。它为多线程环境中的每个线程生成指定类型和指定范围内的随机数。我们可以使用此类的 nextInt() 方法生成随机索引,并从给定的向量访问该索引处的元素。

Java

// Java program to access random elements from the given
// vector using ThreadLocalRandom class
  
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
class GFG {
    
    // Create a Vector instance which accepts elements of
    // String type
    static Vector vector;
    
    // getRandomElements() method which accesses random
    // elements from the given vector
    static void getRandomElements()
    {
        // Run a loop as many times as the number of
        // elements you want to access
        for (int i = 0; i < vector.size(); i++) 
        {
            // Generate a random integer by calling the
            // ThreadLocalRandom.current().nextInt() method
            int index = ThreadLocalRandom.current().nextInt(vector.size());
            
            // Print the element present at this random
            // index in the given vector
            System.out.println(vector.get(index));
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
        vector = new Vector();
        
        // Add elements into the vector
        vector.add("Welcome");
        vector.add("To");
        vector.add("Geeks");
        vector.add("For");
        vector.add("Geeks");
        
        // Call the getRandomElements() method on this
        // vector object
        getRandomElements();
    }
}
输出
Geeks
Welcome
Geeks
To
To