title: Java comparing array element index to index
body: <p>Here's my code:</p>
  
  <pre><code>public static void main(String[] args) {
  
      int lotteryNumbers[] = new int[5];
      int playersNumbers[] = new int[5];
      int winNum = 0;
  
  
      Scanner in = new Scanner(System.in);
      Random r = new Random();
      // array med 5 heltal
  
      //skapa en for loop som fyller upp arrrayen
      //med slumpade tal från klassen Random
      for (int i = 0; i &lt; lotteryNumbers.length; i++) {
          lotteryNumbers[i] = 0 + r.nextInt(9);
          System.out.print(lotteryNumbers[i] + "\t");
  
      }
      System.out.println();
  
      for (int i = 0; i &lt; playersNumbers.length; i++) {
          //System.out.println("Var god ange ett nummer mellan 0-9: ");
          //playersNumbers[i]=in.nextInt();
          playersNumbers[i] = 0 + r.nextInt(9);
  
      }
      for (int i = 0; i &lt; playersNumbers.length; i++) {
  
          System.out.print(playersNumbers[i] + "\t");
  
      }
      System.out.println();
  
      if (lotteryNumbers[0]==playersNumbers[0])
           winNum++;
           if (lotteryNumbers[1]==playersNumbers[1])
                winNum++;
                if (lotteryNumbers[2]==playersNumbers[2])
                     winNum++;
                     if (lotteryNumbers[3]==playersNumbers[3])
                          winNum++;
                          if (lotteryNumbers[4]==playersNumbers[4])
                              winNum++;
  
       System.out.println("You have " + winNum + " winning numbers");
  
  }
  
  }
  </code></pre>
  
  <p>I need to compare the numbers index to index. As you can see I have 5 if statements. I was wondering if I can do this with a loop instead?</p>
  
  <p>I already tried to do as this:</p>
  
  <pre><code>for (int i = 0; i &lt; lotteryNumbers.length; i++) {
  
          for (int j = 0; j &lt; playersNumbers.length; j++) {
  
         if (lotteryNumbers[i] == playersNumbers[j]) {
                  winNum++;
  
              }
          }
      }
  </code></pre>
  
  <p>But this compares every index with every index, not just index to index. I want to know how to write a loop for compare index to index?</p>