In this article, we describe a method that Java randomly disrupts array order. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Import Java.util.Random;
public class Randomsort {
Private Random Random = new Random ();
Array size
private static final int SIZE = 10;
The array to reorder
Private int[] positions = new Int[size];
Public Randomsort () {
for (int index=0; index<size; index++) {
Initializes an array, with the following marked as an element value
Positions[index] = index;
}
Order to print the values of the array
Printpositions ();
}
Reordering
public void Changeposition () {
for (int index=size-1; index>=0; index--) {
Randomly take a value from 0 to the index and exchange it with the element at index
Exchange (Random.nextint (index+1), index);
}
Printpositions ();
}
Swap position
private void Exchange (int p1, int p2) {
int temp = POSITIONS[P1];
POSITIONS[P1] = positions[p2];
POSITIONS[P2] = temp; Better location
}
Print the value of an array
private void Printpositions () {
for (int index=0; index<size; index++) {
System.out.print (positions[index]+ "");
}
System.out.println ();
}
public static void Main (string[] args) {
Randomsort rs = new Randomsort ();
Rs.changeposition ();
Rs.changeposition ();
Rs.changeposition ();
}
}
I hope this article will help you with your Java programming.