title: Array Errors Java
body: <p>When I compile the java code, I met the problem:  </p>
  
  <p>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11</p>
  
  <pre><code>at watercount.trap(watercount.java:41)
  at watercount.main(watercount.java:14)
  </code></pre>
  
  <p>It seems that the Array is not used correctly, but I can not figure out.  Thanks for your help !</p>
  
  <pre><code>import java.util.Arrays;
  
  
  public class watercount {
  
      public static void main (String args[]){
  
      int[] Array = new int[] {1,3,4,6,8,12,2,3,4,5,10};
  
  
      int water_num;
  
      water_num = trap(Array);
  
      System.out.println(Arrays.toString(Array));
  
  
      }
  
  
  
      public static int trap (int[] A) 
  
      { int trap=0;
        int high=0;
  
        if (A==null) return trap;
  
        int []leftHigh = new int[A.length];
        int []rightHigh = new int[A.length];
  
        for(int i=0; i&lt;A.length; i++){
            if(A[i]&gt;high) high = A[i];
            leftHigh[i]=high;  
        }
  
        high=0;
  
        for(int i=A.length;i&gt;=0;i--){
            if(A[i]&gt;high) high = A[i];
            rightHigh[i]=high;  
        }
  
        for(int i=0; i&lt;A.length; i++)
  
        {trap+=Math.min(rightHigh[i],leftHigh[i])-A[i];}
  
       return trap;
  
  
  
      }
  </code></pre>