我试图搜索我的arraylist,“lottoraws”,是否包含我的随机数组c所拥有的整数。我想打印多少个数组c,它们是arraylist。这告诉了有多少纠正到了lottoraw。
//代码:
lottoraws=new ArrayList<int []>();
}
public void addLottoraws(int 5) { // for example 5
int[] a = {};
//------------------------Generate random numbers into arraylist
Random random = new Random();
for (int i = 0; i < 5; i++) {
a = new int[7];
for (int j = 0; j < 7; j++) {
int rand = (int) (random.nextInt(35));
a[j] = rand;
}
lottoraws.add(a);
}
for (int k = 0; k < lottoraws.size(); k++) {
System.out.println(Arrays.toString(lottoraws.get(k)));
}
// -------------------------Generate random numbers into array
int[] c = new int[7];
for (int j = 0; j < 7; j++) {
int rand = (int) (Math.random() * 35 + 1);
c[j] = rand;
}
System.out.println("Dragen rad: \n" + Arrays.toString(c));
//------------------------...--..-.-.-.-.-.---------Show result
答案 0 :(得分:0)
In the below method counterMap will have the number as well its count.
Random random = new Random();
List lottoraws = new ArrayList();
for (int i = 0; i < 5; i++) {
// int[] a1 = new int[7];
for (int j = 0; j < 7; j++) {
int rand = (int) (random.nextInt(35));
// a1[j] = rand;
lottoraws.add(rand);
}
} // at the end of this loop, our list will have 35 elements.
for (int k = 0; k < lottoraws.size(); k++) {
System.out.println(lottoraws.get(k));
}
// -------------------------Generate random numbers into array
int[] c = new int[7];
for (int j = 0; j < 7; j++) {
int rand = (int) (Math.random() * 35 + 1);
c[j] = rand;
} // our array will now have 7 random numbers
System.out.println("Dragen rad: \n" + Arrays.toString(c));
Map counterMap = new HashMap();
for(int i=0;i<lottoraws.size();i++){
int lstNum = (int) lottoraws.get(i);
int searchCount = Arrays.binarySearch(c, lstNum);
if(searchCount>-1){
if(counterMap.containsKey(lstNum)){
// System.out.println("found");
int counterInc = (int)counterMap.get(lstNum);
counterMap.put(lstNum,++counterInc);
} else
counterMap.put(lstNum,1);
}
}
System.out.println(counterMap);