java - Search in Arraylist with content of an array -
java - Search in Arraylist with content of an array -
i´m trying search if arraylist, "lottoraws", contain integers random array, c, have. want print how many numbers of array, c, arraylist. tells how many corrects there lottoraw.
//code:
lottoraws=new arraylist<int []>(); } public void addlottoraws(int 5) { // illustration 5 int[] = {}; //------------------------generate random numbers arraylist random random = new random(); (int = 0; < 5; i++) { = new int[7]; (int j = 0; j < 7; j++) { int rand = (int) (random.nextint(35)); a[j] = rand; } lottoraws.add(a); } (int k = 0; k < lottoraws.size(); k++) { system.out.println(arrays.tostring(lottoraws.get(k))); } // -------------------------generate random numbers array int[] c = new int[7]; (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
in below method countermap have number count. random random = new random(); list lottoraws = new arraylist(); (int = 0; < 5; i++) { // int[] a1 = new int[7]; (int j = 0; j < 7; j++) { int rand = (int) (random.nextint(35)); // a1[j] = rand; lottoraws.add(rand); } } // @ end of loop, our list have 35 elements. (int k = 0; k < lottoraws.size(); k++) { system.out.println(lottoraws.get(k)); } // -------------------------generate random numbers array int[] c = new int[7]; (int j = 0; j < 7; j++) { int rand = (int) (math.random() * 35 + 1); c[j] = rand; } // our array 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);
java arrays arraylist
Comments
Post a Comment