title: ArrayList Java- Size is Zero
body: <p>My program (included below) keeps coming up with this error that the list size is zero, and that's messing up everything else.  I'm guessing that means I didn't add the elements in correctly, but I can't figure out what's wrong with it.  I'm technologically challenged, so I'm sorry that this isn't formatted right.  Thanks for your help!</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.ArrayList;
  public class ListStat {
      public static void main (String[] args) {
          ListStat r = new ListStat();
          System.out.println("A default list was created." + r.toString());
          ListStat r1 = new ListStat(30);
          System.out.println("Another list was created." + r1.toString());
          ListStat r2 = new ListStat(40);
          System.out.println("Another list was created." + r2.toString());
          r1.add(4); 
          System.out.println("The item at index 13 for r1 is: " + r1.get(13));
          System.out.println("r1 and r2 are equal: " + r1.equals(r2));
          System.out.println("r1 and r2 overlap this number of times: " + r1.countOverlap(r1, r2));
      }
  
  private ArrayList&lt;Integer&gt; list;
  
  public ListStat() {
  list = new ArrayList&lt;Integer&gt;();
  }
  
  public ListStat(int N) {
  list = new ArrayList&lt;Integer&gt;(N);
  for (int i=0; i&lt;N; i++) {
      list.add(i, i+1);
  }
  }
  
  public void add(int num) {
  list.add(num);
  }
  
  public int get(int index) {
  return list.get(index);
  }
  
  public int getListLength(){
  return list.size();
  }
  
  public double getMean() {
  int sum = 0;
  for (int i=0; i&lt;list.size(); i++) {
      sum = sum + list.get(i);
  }
  double mean = sum/list.size();
  return mean;
  }
  
  public int getSum() {
  int sum = 0;
  for (int i=0; i&lt;list.size(); i++) {
      sum = sum + list.get(i);
  }
  return sum;
  }
  
  public boolean equals(ListStat r) {
  if (r.equals(list))
  return true;
  else
  return false;
  }
  
  public static int countOverlap(ListStat r1, ListStat r2) {
  int count = 0;
  for (int i=0; i&lt;r1.getListLength(); i++) {
      for (int j=0; j&lt;r2.getListLength(); j++) {
          if (r1.get(i)==r2.get(j)) {
              count++;
          }
      }
  }
  return count;
  }
  
  public double getStdDev() {
  int sum = 0;
  double standardDev = 0.0;
  for (int i=0; i&lt;list.size(); i++) {
      sum = sum + list.get(i);
  }
  double mean = sum/list.size();
  double standsum=0;
  for (int i=0; i&lt;list.size(); i++) {
      standardDev = (list.get(i) - mean) * (list.get(i) - mean);
      standsum=standsum+standardDev;
  }
  standsum = standsum/list.size();
  standsum = Math.sqrt(standsum);
  return standsum;
  }
  
  public String toString() {
  return "The list length is " + getListLength() + ", the mean is " + getMean() + ", the sum is " + getSum() + ", and the standard deviation is " + getStdDev();  
  }
  }
  </code></pre>