title: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
body: <p>Still have problem with java :/
  So here is my problem with list :</p>
  
  <pre><code>if(listKey.size() == 1){
          k = listKey.get(0);
  }else if(listKey.size() &gt; 1){
          for(int q : listKey){
              if(listKey.get(q) == listKey.size()){
                  k = listKey.get(0);
                  k1 = listKey.get(1);
              }
              else if(listKey.get(q) == listKey.size() -1 ){
                  k = listKey.get(q+1);
                  k1 = listKey.get(0);
  
              }
              else if(listKey.get(q) == key &amp;&amp; listKey.get(q) != listKey.size()){
                  k = listKey.get(q+1);
                  k1 = listKey.get(q+2);
  
              }
          }
  
  
  }else if(listKey.size() == 0){
          System.out.println("empty");
      }
  </code></pre>
  
  <p>It gives me this error:</p>
  
  <pre><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2
  at java.util.ArrayList.rangeCheck(ArrayList.java:635)
  at java.util.ArrayList.get(ArrayList.java:411)
  at com.server.PeerNode.findReplicas(PeerNode.java:222)
  at com.server.PeerNode.printMenu(PeerNode.java:154)
  at com.server.PeerNode.run(PeerNode.java:99)
  at com.server.Main.main(Main.java:64)
  </code></pre>
  
  <p>I think i'm using correctly the List and how to iterate through it! I'm not exceeding the size of the list when i try to get int from it.
  Thanks in advance for help</p>
  
  <pre><code>Here is my idea: I got a key = 5 
  
  case 1: array {4}... i just want to put 4 into k
  case 2: array {4,6}.. i just to want to put k=6 and k1=4
  case 3: array {4,6,7}.. i just want to put k=6 and k1=7
  </code></pre>
  
  <p>I know is mess up my code... and also with some erros. Thanks to hugh i fixed it and now works... it's like that now:</p>
  
  <pre><code>    if(listKey.size() == 1){
          keyreplica1 = listKey.get(0);
  
      }else if(listKey.size() &gt; 1){
          listKey.add(key);
          Collections.sort(listKey);
          for (int q=0; q&lt;listKey.size(); q++){
              if(listKey.get(q) == key &amp;&amp; listKey.get(q) == listKey.size()){
                  keyreplica1 = listKey.get(0);
                  keyreplica2 = listKey.get(1);
  
              }
              else if(listKey.get(q) == key &amp;&amp; listKey.get(q) == listKey.size() -1 ){
                  keyreplica1 = listKey.get(q+1);
                  keyreplica2 = listKey.get(0);
  
              }
              else if(listKey.get(q) == key &amp;&amp; listKey.get(q) != listKey.size()){
                  keyreplica1 = listKey.get(q+1);
                  keyreplica2 = listKey.get(q+2);
  
              }
          }
  
      }else if(listKey.size() == 0){
  
      }
  </code></pre>