Java中 ArrayList 的随机元素
Shuffle 是指随机改变 ArrayList 元素的位置。洗牌后,它们将处于不同的顺序。
以下是混洗 ArrayList 元素的示例。
排列 ArrayList 元素的方法:
- 使用 Random 类
- 使用 Collections.shuffle()
方法 1:使用Random 类
- 在此方法中,我们将使用 Random 类对 ArrayList 元素进行混洗以生成随机索引。
- 和Java collections.swap() 方法来交换 ArrayList 元素。
- 我们将要使用的另一种算法是Fisher-Yates shuffle。
代码:
Java
// Java program to demonstrate shuffling of arraylist
// elements Using Random class
import java.util.*;
class ArraylistShuffle {
public static void main(String args[])
{
// creating
// ArrayList
ArrayList al = new ArrayList();
// adding object in ArrayList
al.add(10);
al.add(20);
al.add(30);
al.add(40);
al.add(50);
al.add(60);
al.add(70);
al.add(80);
System.out.println("Before shuffling Arraylist:");
// getting Iterator
// from arraylist to
// traverse elements
Iterator itr = al.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
System.out.println("");
Random r1 = new Random();
for (int i = al.size() - 1; i >= 1; i--) {
// swapping current index value
// with random index value
Collections.swap(al, i, r1.nextInt(i + 1));
}
System.out.println("After shuffling Arraylist:");
itr = al.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
}
}
Java
// Java program to demonstrate shuffling
// ArrayList elements using Collections.shuffle()
import java.util.*;
class ArraylistShuffle {
public static void main(String args[])
{
// creating ArrayList
ArrayList al = new ArrayList();
// adding object in ArrayList
al.add("C");
al.add("C++");
al.add("Java");
al.add("Python");
al.add("PHP");
al.add("Javascript");
System.out.println("Before shuffling Arraylist:");
// getting Iterator
// from arraylist to
// traverse elements
Iterator itr = al.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
System.out.println("");
Collections.shuffle(al);
System.out.println("After shuffling Arraylist:");
itr = al.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
}
}
输出
Before shuffling Arraylist:
10 20 30 40 50 60 70 80
After shuffling Arraylist:
10 60 30 20 50 80 40 70
方法 2:使用Collections.shuffle()
使用 Collections.shuffle() 随机播放 ArrayList 元素。
Java
// Java program to demonstrate shuffling
// ArrayList elements using Collections.shuffle()
import java.util.*;
class ArraylistShuffle {
public static void main(String args[])
{
// creating ArrayList
ArrayList al = new ArrayList();
// adding object in ArrayList
al.add("C");
al.add("C++");
al.add("Java");
al.add("Python");
al.add("PHP");
al.add("Javascript");
System.out.println("Before shuffling Arraylist:");
// getting Iterator
// from arraylist to
// traverse elements
Iterator itr = al.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
System.out.println("");
Collections.shuffle(al);
System.out.println("After shuffling Arraylist:");
itr = al.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
}
}
输出
Before shuffling Arraylist:
C C++ Java Python PHP Javascript
After shuffling Arraylist:
PHP Java C++ C Javascript Python