title: how to find second largest index of arraylist in java
body: <p>I have to write a method that returns the second largest index of an ArrayList of integer values.</p>
  
  <p>The array list is:
  4 8 15 16 23 42 97 56 95 85 63 41 52 99 97 Q</p>
  
  <p>The Q is there to mark the end of the input. I am using fileIn.hasNextInt() to read the input and check to see if it is an integer.</p>
  
  <p>The problem I am having with my logic is it is only cycling through the ArrayList and returning the last index of the ArrayList and not the index of the second largest value.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    public static int secondMaxIndex(ArrayList&lt;Integer&gt; intArray){
          int largest  = intArray.get(0);
          int largest2 = intArray.get(0);
          int maxIndex2 = 0;
          for( int i = 0; i &lt;= intArray.size() - 1; i++){
              if( largest &lt; intArray.get(i) ){
                  largest = intArray.get(i);
              }
          }
          for( int j = 0; j &lt;= intArray.size() - 1; j++){
              if( intArray.get(j) &lt; largest ){
                  maxIndex2 = j;
              }
          }
          return maxIndex2;
      }
  </code></pre>