title: Infinite Loop when using vector.size() in for loop's condition statement
body: <p>I was implementing Bucket Sort and using insertion sort for sorting each bucket.
  But when I ran the code it went into an infinite loop. After debugging, I found that the for loop was running infinitely. </p>
  
  <pre><code>// we have buckets for each intervals
  // we add values into each bucket 
  // each bucket is then sorted at the end
  // then all the buckets are merged together
  public class BucketSort {
  
  
  @SuppressWarnings("unchecked")
  private Vector&lt;Double&gt;[] buckets = new Vector[10];
  
  
  private int sizeOfInput;
  
  //constructor
  
  public BucketSort(int size){
      sizeOfInput = size;
      for(int i=0; i&lt;10;i++){
          buckets[i] = new Vector();
  
      }
  
  }
  
  
  public static void main(String args[]){
      Double[] arr =  {0.11,0.12,0.21,0.61,0.7,0.5,0.14,0.2,0.61,0.65,0.72,0.80,0.98,0.82,0.96,0.35,0.47,0.53};
  
      BucketSort s = new BucketSort(arr.length);
  
      Vector result = s.bucketSort(arr);
      System.out.println("The result is : ");
  
      for(int i= 0;i&lt; result.size();i++){
              System.out.println(result.get(i));
      }
      //s.printBucket();
  } 
  
  public void printBucket(){
      // prints out the elements in each bucket 
      for(int i = 0; i&lt;10;i++){
          System.out.println("The bucket "+ i + " contains these elements");
          for(int j=0; j&lt;buckets[i].size();j++){
  
                  System.out.println(buckets[i].get(j));
          }
      }
  }
  
  
  public Vector bucketSort(Double[] arr){
      // add the elements in appropriate buckets
      for(int i = 0; i&lt;arr.length;i++){
          for(int j=0; j&lt;10;j++){
              if(arr[i] &lt; (double)(j+1)/10){
                  buckets[j].add((double)arr[i]);
                  break;
              }
          }
      }
  
      // print out each bucket
      printBucket();
  
      // call the sort function on each of the buckets
      for(int i= 0 ; i&lt; 1; i++){
  
          sort(1);
          System.out.println("Sorted bucket" + i);
      }
  
  
      return merge();
  }
  
  public void sort(int number){
  
      //binary sort each values of vector
      System.out.println("bucket" + number + "size is " + buckets[number].size());
      int k = buckets[number].size();
  
      for(int i = 1; i &lt; (buckets[number].size()); i++ ){
          System.out.println("Bucket" + number + "element" + i);
  
          double a = buckets[number].get(i); 
          int j= i;
          while(j &gt; 0 &amp;&amp; buckets[number].get(j-1) &gt; a){
                  //double b = buckets[no].get(j-1);
                  //buckets[no].add(j,b);
                  j--;
                  System.out.println("Sorting bucket" + number);
          }
  
          buckets[number].add(j,a);           
  
      }
  }
  
  public Vector merge(){
      //merge all the bucket vectors into an array
      Vector&lt;Double&gt; result = new Vector();
  
          for(int j=0;j&lt;10;j++){
              //buckets[j].copyInto(result);
              result.addAll(buckets[j]);
          }
  
      return result;
  }   
  </code></pre>
  
  <p>}</p>
  
  <p>If I replace</p>
  
  <pre><code>for(int i = 1; i &lt; (buckets[number].size()); i++ ){
  </code></pre>
  
  <p>with </p>
  
  <pre><code>for(int i = 1; i &lt; k; i++ ){
  </code></pre>
  
  <p>where k = buckets[number].size()</p>
  
  <p>This solved the problem but I didn't understand why did the error occur in the first place.
  Please explain the reason for such inappropriate behavior.</p>