Topic: 下标

  Print this page

1.下标 Copy to clipboard
Posted by: 丫丫
Posted on: 2007-10-26 15:47

public class Char
{
  public static void main(String[] args)
  {
    int []x={10,20,30,40};
    for (int i = 1; i<=4; i++)
    {
      try{
        System.out.println (x[i]);
      }
      catch(Exception ex){
        System.out.println ("异常信息:"+i+"超出数组下标范围");
      }
      
    }
      
    System.out.println("程序运行结束!");
  }
}

2.Re:下标 [Re: 丫丫] Copy to clipboard
Posted by: zyj0021
Posted on: 2007-10-27 08:23

数组下标 i 应该从0开始!!!!
你的数组X[4]就会超出范围!

3.Re:下标 [Re: 丫丫] Copy to clipboard
Posted by: chaostring
Posted on: 2007-11-04 21:25

System.out.println (x[i]);
改成
System.out.println (x[i-1]);
应该就不会抛异常了


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923