Java Learning Diary-basis-the remainder operation of random array

Source: Internet
Author: User

Time is May 6, 2017, Youth Day just past, the coordinates of Shanghai and Shanghai Drift 2 months.

Let's start with the brief. By:slowcity

"Case Study 1"

Requires the definition of an int array A, containing 100 elements, to hold 100 random 4-digit numbers. Then define an int type array B, which consists of 10 elements. Statistics a array of elements to 10 to find the remainder equals 0 of the number, save to b[0], 10 for the remainder equals 1 of the number, saved to b[1], ... And so on

Generally see this kind of the most basic idea:
1 Create a random four-digit number with an array
2 retrieving an array, counting the conditional operations

4 Output Print new array
The beginning of my-Rookie beginner's idea: Read the code feel too upright boy

1 Import Java.util.Random;
2  Public classDemo1 {3      Public Static  voidMain (String[]args) {4         inta[]=New int[100];5         intB1=0, b2=0,b3=0,b4=0,b5=0,b6=0,b7=0,b8=0,b9=0,b10=0;6          for(inti=0;i<a.length;i++){7             intmax=9999;//generates 100 4-bit random numbers and saves them in an array8                 intmin=1000;9Random random =NewRandom ();Ten                 ints = random.nextint (max)% (max-min+1) +min; Onea[i]=s;  ASystem.out.print (a[i]+ ""); -             if(A[i]/10==0) {//adds an array of elements to the 10 remainder operation and counts -b1++; the System.out.print (B1); -}Else if(a[i]%10==1){ -b2++; -}Else if(a[i]%10==2){ +b3++; -}Else if(a[i]%10==3){ +b4++; A}Else if(a[i]%10==4){ atb5++; -}Else if(a[i]%10==5){ -b6++; -}Else if(a[i]%10==6){ -b7++; -}Else if(a[i]%10==7){ inb8++; -}Else if(a[i]%10==8){ tob9++; +}Else if(a[i]%10==9){ -b10++; the                 } *         } $         intb[]=New int[10];//defining a new array, storing the countPanax Notoginsengb[0]=B1; -b[1]=B2; theb[2]=B3; +b[3]=b4; Ab[4]=b5; thed[s]=b6; +b[6]=B7; -b[7]=B8; $b[8]=B9; $b[9]=B10; -          for(intj=0;j<b.length;j++){ -System.out.println (B[j]);//use a For loop to print a new array the         }  -     }Wuyi}

After optimization

"Optimize One"
In comparison, the length of the code is less more than half, the algorithm is more logical
1 Public classDemo2 {2 Public Static voidMain (String[]args) {3 inta[]=New int[100];//declaring an array of length 100 a4 intb[]=New int[10];//declaring an array of length 10 B5 for(inti=0;i<a.length;i++) {//generate 4-bit numbers, stored in a array6A[i]= (int) (10000*math.random ());7 }8 for(intj=0;j<b.length;j++) {//Loop Body Nesting9 intSum=0;Ten for(inti=0;i<a.length;i++){ One if(a[i]%10==j) { Asum++; - } - } theb[j]=sum; -System.out.println (B[j]);//Print Array b - } - } +}
"Optimize two"

1 ImportJava.util.Random;2 3 Public classradomdemo{4 Public Static voidMain (string[] args) {5 int[] A=New int[100];6 int[] b=New int[10];7Random ran=NewRandom (); Define the Run method-Generate random numbers8 intIndex=0;9 while(index<100) {//use while for conditional filteringTen inttmp=Ran.nextint (); Generates a random number, and the filter---here is much slower when the code runs One if((tmp<=9999&&tmp>=1000)){ Aa[index]=tmp; To store a filtered random number -Index + +; - SYSTEM.OUT.PRINTLN (TMP); The output here is not necessary the } - } -System.out.println ("-----------------------------------"); - for(intj=0;j<10;j++) {//Nested loops + intNum=0; - for(inti:a) {//for-each Enhanced statement, traversing array + if(i%10==j) { Anum++; at } - } -b[j]=num; Assign a count result to data b - System.out.println (B[j]); - } - } in}

Learning Summary: The method is very important, the most important is to simplify the algorithm, condensed code.

Java Learning Diary-basis-the remainder operation of random array

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.