title: Problem with the ArrayList class in Java
body: <p>I have a problem with the following code. I obtain the error message</p>
  
  <pre><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
      at java.util.ArrayList.rangeCheck(ArrayList.java:571)
      at java.util.ArrayList.set(ArrayList.java:364)
      at Test.main(Test.java:17)
  </code></pre>
  
  <p>and I don't understand why. I have a list, which is initialised, then I iterate over it, and then I want to fill another list with the values of the first list. I don't understand why I get IndexOutOfBoundsException. It seems that my initialisation is not correct. Thanks a lot in advance.  </p>
  
  <pre><code>public static void main(String[] args) {
  
          String s1 = "one";
          String s2 = "one";
          ArrayList list = new ArrayList();
          list.set(0, s1);
          list.set(1, s2);
          Iterator it = list.iterator();
          ArrayList listToFill = new ArrayList();
          int k = 0;
          while (it.hasNext()) {
              String m = "m";
              listToFill.set(k, m);
              k++;
          }
  
      }
  </code></pre>