40. 编写一个冒泡法排序程序,要求在程序中能够捕获到数组下标越界的异常。
  以下是代码:int[] a = new int[5];
            try
            {
                for (int k = 0; k < 5; k++)
                {
                    a[k] = int.Parse(Console.ReadLine());
                }
                for (int j = 0; j < a.Length - 1; j++)
                {
                    for (int i = 0; i < a.Length - 1; i++)
                    {
                        int t;
                        if (a[i] > a[i + 1])
                        {
                            t = a[i];
                            a[i] = a[i + 1];
                            a[i + 1] = t;
                        }
                    }
                }            }
            catch (Exception)
            {
                Console.WriteLine("数组下标越界");
                return;
            }
            foreach (int z in a)
            {
                Console.WriteLine(z);
            }
我运行感觉怎么没捕捉到了,求解答

解决方案 »

  1.   

    数组下标越界,
    是不是就是int[] a = new int[5];
    int b=a[6];
    索引超出了数组界限?
    LZ,你for循环里 一个个自己 让下算,如果[]大于5 就报异常了。
      

  2.   


    int[] a = new int[5];
                try
                {
                    for (int k = 0; k < 5; k++)
                    {
                        a[k] = int.Parse(Console.ReadLine());
                    }
                    for (int j = 0; j < a.Length; j++)
                    {
                        for (int i = 0; i < a.Length; i++)
                        {
                            int t;
                            if (a[i] > a[i + 1])
                            {
                                t = a[i];
                                a[i] = a[i + 1];
                                a[i + 1] = t;
                            }
                        }
                    }            }
                catch (IndexOutOfBoundsException ex)
                {
                    Console.WriteLine(ex);
                    return;
                }
      

  3.   

    错误 1 找不到类型或命名空间名称“IndexOutOfBoundsException”(是否缺少 using 指令或程序集引用?) D:\C#练习\ConsoleApplication1\ConsoleApplication1\Program.cs 434 20 ConsoleApplication1
    报错啊,杂回事!不过还是感谢了!
      

  4.   

     catch (IndexOutOfRangeException ex)
                {
                    Console.WriteLine(ex);
                    return;
                }或者换成Exception
      

  5.   

    catch ( IndexOutOfRangeException  ex)
                {
                    Console.WriteLine(ex);
                    return;
                }或者换成Exception
      

  6.   

    愁死我了,我看了半天,程序没什么问题啊,结果好好看看lz的话,是想把这段‘好’的代码变成有问题的,囧!
    直接输出a[5],不就得了!
      

  7.   


    IndexOutOfBoundsException换成IndexOutOfRangeException
      

  8.   

    楼上的 把 第二次循环 些成了 死的 也不是很好的选择 看看我这个吧
    for(int i=0;i<a.Length;i++)
    {
       for(int j=a.Length;j>i;j--)
        {
           if(a[i]>a[j])
            {
               这里的你就自己写吧
             }
        }
    }
      

  9.   

    看 这个 
    static List<int> list = new List<int>() { 72, 54, 59, 30, 31, 78, 2, 77, 82, 72 };
         
    static void Main(string[] args)
    {
        Bubble();
        PrintList();
    }
     
    static void Bubble()
    {
        int temp = 0;
        for (int i = list.Count; i > 0; i--)
        {
            for (int j = 0; j < i - 1; j++)
            {
                if (list[j] > list[j + 1])
                {
                    temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
            }
            PrintList();
        }
    }
     
    private static void PrintList()
    {
        foreach (var item in list)
        {
            Console.Write(string.Format("{0} ", item));
        }
        Console.WriteLine();
    }
      

  10.   

    i 和 j 的范围都是 [0, (Length - 2)],注意,你的判断是 < Length - 1,当前 length 是 5,所以 i 和 j 的范围就是 0 ~ 3,加个 1,范围就是 1 ~ 4,怎么都越不了界啊。
      

  11.   

    冒泡法  确实错了  排不了顺序
    正确的应该是:
     for(int j=0;j<a.Length-1;j++)
     {
        for(int i=j+1;i<=a.Length-1;i++)
          {
             int t;
              if(a[j]>a[i])//从小到大排序
               {
                 t=a[j];
                a[j]=a[i];
                a[i]=t;  
               }
              
          } }
    你里面循环条件都是没有等号,所以
    k的范围0~4
    i的范围0~3  i+1的范围1~4
    j的范围0~3 
    而a数组范围0~4
    所以嘛  你程序根本就没越界当然没捕捉到
      

  12.   

    你的算法没有错,而且不会触发数组下表越界这个异常。
    for (int k = 0; k < 5; k++)
    {
       a[k] = int.Parse(Console.ReadLine());
    }
    这就是个长度为5的数组,根本就不会越界。
    for (int k = 0; k < 6; k++)
    {
       a[k] = int.Parse(Console.ReadLine());
    }
    这样才会有这个异常