2015-04-04 86 views
0

我试图搜索我的数组列表“lottoraws”是否包含我的随机数组c,具有的整数。我想打印多少数字的数组,c,这是数组列表中的数组。它告诉了Lottoraw有多少修正。在Arraylist中搜索数组的内容

//代码:

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 
+2

什么是你的问题? – 2015-04-04 10:47:33

+0

你只需要普通数字或数字本身的数量 – 2015-04-04 10:50:22

+0

我只想搜索“c”中的数字,然后打印数列中的数字。所以只打印countervalue。它可能在0-7之间的值上打印,具体取决于randomgen。 – Johan 2015-04-04 10:53:32

回答

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); 
+0

好的谢谢,但有没有办法做到这一点,而不使用Hashmap? @Saurabh Jhunjhunwala – Johan 2015-04-04 13:03:11

+0

你可以通过使用与“c”等同的数组来避免映射。用零初始化所有字段并递增存在lstNum的字段。一旦循环终止,您需要运行另一个循环,并且只滤出非零字段将其映射到“c”并找出每个数字的出现。 这将是复杂的,因此我更喜欢地图。 – 2015-04-04 13:07:30

+0

也许是这样的:\t \t \t \t int count1 = 0; \t \t对(INT J = 0;Ĵ Johan 2015-04-04 13:08:35