title: How do I average the contents in ArrayList?
body: <p>This TestCode is supposed to create an stream of numbers in seconds.
  Collect 10 samples, and average the time which each samples comes out.
  I did try to use <em>if-else</em>, but the variable from <em>if</em> doesn't share with <em>else</em>.
  Please correct me if I'm wrong.
  I don't understand lambda just yet.</p>
  <pre><code>public class TestCode {
  
      private int eachTwoSec;
  
      // supposed to aList.add 10 items
      // average the time needed in between each aList.add (2 obviously)
      public void avgTimeTaken() {
          ArrayList aList = new ArrayList();
          for (int i = 0; i &lt; 10; i++) {
              aList.add(eachTwoSec);
          }
      }
  
      // return a number every two seconds (endless stream of samples)
      // samples 50,52,54,56,58,60,2,4,6,8,10
      public void twoSecTime() {
          try {
              Thread.sleep(2000);
          } catch (InterruptedException ex) {
              Logger.getLogger(Dummies.class.getName()).log(Level.SEVERE, null, ex);
          }
  
          LocalDateTime ldt = LocalDateTime.now();
          DateTimeFormatter dtf = DateTimeFormatter.ofPattern(&quot;ss&quot;);
          eachTwoSec = Integer.parseInt(ldt.format(dtf));
          System.out.println(eachTwoSec);
          twoSecTime();
      }
  
      public TestCode() {
          // construct
          avgTimeTaken();
          new Thread(this::twoSecTime).start();
      }
  
      public static void main(String[] args) {
          // just a start point
          new TestCode();
      }
  }
  </code></pre>