java中的map的簡單應用之陣列元素個數
package Test01;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import javax.naming.ldap.Rdn;
public class ComTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Random random=new Random();
System.out.println(“輸入測試數字的總個數:”);
int n=input.nextInt();
System.out.println(“輸入數字的範圍”);
int bround=input.nextInt();
Map<Integer, Integer> myMap=new HashMap<>();
for(int i=0;i<n;i++) {
//int temp=input.nextInt();
int temp=random.nextInt(bround);
if(!(myMap.containsKey(temp))) {
myMap.put(temp, 1);
}
else {
int iCount=myMap.get(temp);
iCount++;
myMap.put(temp, iCount);
}
}
for(Map.Entry<Integer, Integer> me :myMap.entrySet()) {
int key=me.getKey();
int value=me.getValue();
System.out.printf(“key:%d value:%d\n”, key,value);
}
input.close();
}
}