A method for Java randomly disrupting array order _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.