您的位置:首页 > 其它

数组(静态初始化-常见问题)

2014-06-25 16:08 211 查看


class Demo1
{
public static void main(String[] args)
{
//数组的两种写法
int[] x = new int[2];
int[] y = new int[]{12,23};
//如果使用第一种的话,就要给他们赋值
x[0] = 12;
x[1] = 42;
System.out.println(x[1]+"+"+y[1]);
}
}


ArrayIndexOutOfBoundsException     操作数组是,访问到了数组中不存在的角标,角标越界异常
NullPointerException     空指针异常

class Demo2
{
public static void main(String[] args)
{
int[] x = new int[3];
System.out.println(x[3]);//显示ArrayIndexOutOfBoundsException 3

x = null;
System.out.println(x[1]);//显示NullPointerException
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string 指针