Daily Record-

Source: Internet
Author: User

Today is an integer random replacement.

Remember: in Java, the basic data type is to pass the value, not to pass the reference.

1 package random replacement; 2 Import Java. util. *; 3 4 // generate random replacement of the First n integers 5 // first fill the entire array in order, then switch one by one to the previous random position. 6 7 public class randomexchange {8 9 public static void main (string [] ARGs) {10 // todo auto-generated method stub11 int [] A = suijizhihuan (6); 12 for (INT I = 0; I <. length; I ++) {13 system. out. print (A [I] + ","); 14} 15} 16 17 Private Static int [] suijizhihuan (INT N) {18 int [] suiji = new int [N]; 19 for (INT I = 0; I <n; I ++) {20 Suiji [I] = I + 1; 21} 22 random ran = new random (); 23 // I should start from 1 !!! 24 For (INT I = 1; I <n; I ++) {25 swap (suiji, I, ran. nextint (I); 26} 27 return suiji; 28 29} 30 // direct int temp = A; A = B; B = temp; the swap function of is not feasible. 31 // because the basic data type in Java is to pass the value rather than to pass the referenced 32 //, if an array exists, write the swap function 33 Private Static void swap (INT [] arr, int I, Int J) {34 int temp = arr [I]; 35 arr [I] = arr [J]; 36 arr [J] = temp; 37 38} 39 40}

 

Daily Record-

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.