//app9_1.java
class app9_1
{
public static void main(String args[])
{
int i;
int a[]={1,2,3,4};
for(i=0;i<5;i++)
System.out.println("  a["+i+"]="+a[i]);
}
}
数组越界.....
如何处理

解决方案 »

  1.   


    public static void main(String args[])
        {
            int i;
            int a[]={1,2,3,4};
            for(i=0;i<5;i++)
            {
             try{
             System.out.println("  a["+i+"]="+a[i]);
             }catch(ArrayIndexOutOfBoundsException e)
             {
             System.out.println("数组越界了");
             }
            }
        }
      

  2.   

    class app9_1
    {
        public static void main(String args[])
        {
            int i;
            int[] a={1,2,3,4};
            for(i=0;i<a.length;i++)
                System.out.println("  a["+i+"]="+a[i]);
        }
    }该成这样就出不了那种异常了
      

  3.   

    try
    {
      for(i=0;i<5;i++)
                System.out.println("  a["+i+"]="+a[i]);
    }
    catch(IndexOutOfBoundsException e)
    {
      System.out.println("数组越界");
    }
    catch(Exception ee)
    {
      System.out.println("其他异常");
    }
      

  4.   

    ArrayIndexOutOfBoundsException 是运行时异常,完全可以使用
    for (int i = 0; i < a.length; i++) ...解决,
    不应该捕获。
      

  5.   

    int a[]={1,2,3,4};
            for(i=0;i<5;i++)数组的下标从0开始,所以你的数组的最大下标是3
    for(i=0;i<=3;i++){
      

  6.   


    public static void main(String args[])
    {
        int i;
        int a[]={1,2,3,4};
        for(i=0;i<5;i++)
        {
         try{
          System.out.println("  a["+i+"]="+a[i]);
         }catch(ArrayIndexOutOfBoundsException e)
         {
         System.out.println("出界了");
         break;
         }
        }
           
    }
      

  7.   

    这是runtime exception,出这个exception基本上是程序有问题