Created
July 12, 2017 14:59
-
-
Save JiaruiTracy/ff184386b19486e18aec5f9aad8502bc to your computer and use it in GitHub Desktop.
A quick way to calculate repeat times of each element in an array, which contains 100 random numbers between 0 and 30
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package arr; | |
import java.util.Random; | |
public class arrTest { | |
public static void main(String[] args) { | |
int[] randNum = new int[100]; | |
int[] count = new int[31]; | |
Random r = new Random(); | |
for(int i = 0;i<100;i++){ | |
randNum[i] = r.nextInt(30)+1; | |
System.out.print(randNum[i]+"."); | |
count[randNum[i]]++; | |
} | |
System.out.println(); | |
for(int i=1;i<31;i++){ | |
System.out.print(count[i]+","); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment