title: Passing an ArrayList of any type to a method
body: <p>I'm working on my first Android app, a math game for my kid, and am learning Java in the process.  I have two <code>ArrayList</code>s, one of integers <code>0..9</code> and one strings of possible operations, at present, just <code>+</code> and <code>-</code>.</p>
  
  <p>I would like to write a method that returns a random index into an <code>ArrayList</code>, so I can select a random element.  What I'm running into is that I need two methods, one for each type of <code>ArrayList</code>, even though the code is identical.  Is there a way to do this in a single method?</p>
  
  <p>What I use now:</p>
  
  <pre><code>Random randomGenerator = new Random();
  
    . . .
  
  n = randomIndexInt(possibleOperands);
  int op1 = possibleOperands.get(n);
  n = randomIndexInt(possibleOperands);
  int op2 = possibleOperands.get(n);
  n = randomIndexStr(possibleOperations);
  String operation = possibleOperations.get(n);
  
      . . .
  
  int randomIndexInt(ArrayList&lt;Integer&gt; a){
      int n = randomGenerator.nextInt(a.size());
      return n;
  }
  
  int randomIndexStr(ArrayList&lt;String&gt; a){
      int n = randomGenerator.nextInt(a.size());
      return n;
  }
  </code></pre>
  
  <p>What I'd like to do is collapse <code>randomIndexInt</code> and <code>randomIndexStr</code> into a single method.</p>