In Java, you can use
collections.shuffle
to clean or rerun a ArrayList
TestApp.java
package com.mkyong.utils;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class TestApp {
public static void main(String[] args) {
List<String> list = Arrays.asList("A", "B", "C", "D", "1", "2", "3");
//before shuffle
System.out.println(list);
// again, same insert order
System.out.println(list);
// shuffle or randomize
Collections.shuffle(list);
System.out.println(list);
System.out.println(list);
// shuffle again, different result
Collections.shuffle(list);
System.out.println(list);
}
}
Output
[A, B, C, D, 1, 2, 3]
[A, B, C, D, 1, 2, 3]
[2, B, 3, C, 1, A, D]
[2, B, 3, C, 1, A, D]
[C, B, D, 2, 3, A, 1]
References
- Collections.shuffle JavaDoc
Read More:
- An error was reported when idea compiles Java: no symbol was found_ How to solve this problem
- Java retainAll throws an unsupported operation exception record
- When using idea to start a project, an error is reported: Error:java : Compilation failed: internal java compiler error
- Winsw throws an exception “error 1067: unexpected process termination” when converting Java application to Windows Service
- How to open an app from an unidentified developer
- How to exit jshell in Java?
- Java uses ZipFile to decompress the zip file and runs normally under Windows. Linux reports an error
- The @ Autowired annotation in springboot is invalid in ordinary classes. How to solve and use the null pointer exception java.lang.nullpointerexception
- Java uses Netease mailbox 163 to send an error message
- java.lang.IllegalArgumentException: Address 127.0.0.1:5672:5672 seems to contain an unquoted IPv6
- How to use scanner to accept char type characters in Java
- Java uses the createnewfile() method to report an error
- java.lang.UnsupportedOperationException resolvent
- How To Fix Windows Java Error 1723
- How to get the current time in java time string
- How to set JAVA_HOME on mac
- How to generate UML Diagrams from Java code in Eclipse
- When Dbeaver starts on Mac, it reports an error: “fail to create java virtual machine”
- An error is reported during Java operation due to the problem of static resource export
- How to generate main () method in java graphical interface