Java数组越界问题实例解析
作者:inione 时间:2023-10-25 18:16:23
Java中数组初始化和OC其实是一样的,分为动态初始化和静态初始化,
动态初始化:指定长度,由系统给出初始化值
静态初始化:给出初始化值,由系统给出长度
在我们使用数组时最容易出现的就是数组越界问题,好了,这里有个简单的例子
int [][] array = {{1,2,3},{1,4}};
System.out.println(array[1][2]);
这是一个二维数组,很明显,数组越界了,控制台中会打印如下信息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at demo.Array.main(Array.java:31)
很准确的定位到31行。
下面看看一个完整的代码示例:
package home_work_4_17;
abstract class Employee{
abstract double earnings();
}
class YearWorker extends Employee{
double earnings(){
System.out.println("按年领取1000");
return 1000;
}
}
class MonthWorker extends Employee{
double earnings(){
System.out.println("按月领取100");
return 100;
}
}
class WeekWorker extends Employee{
double earnings(){
System.out.println("按星期领取10");
return 10;
}
}
class Company{
int n;
//该公司的人数
public Company(int n) {
this.n=n;
// TODO Auto-generated constructor stub
}
Employee E[]=new Employee[n];
double cal(){
//返回薪水综合
double sum=0;
for (int j=0;j<n;j++){
sum+=E[j].earnings();
}
return sum;
}
}
public class work_2 {
public static void main(String args[]) {
Company c=new Company(3);
c.E[0]=new WeekWorker();
c.E[1]=new MonthWorker();
c.E[2]=new YearWorker();
System.out.println("总支出:"+c.cal());
}
}
编译器显示数组越界错误。
经检查发现划线部分语句出错
应当做如下修改:
class Company{
int n; //该公司的人数
Employee E[];
public Company(int n) {
this.n=n;// TODO Auto-generated constructor stub
E=new Employee[n];
}
double cal(){ //返回薪水综合
double sum=0;
for(int j=0;j<n;j++){
sum+=E[j].earnings();
}
return sum;
}
}
出错原因是:当Company构造方法中并未对数组E进行初始化操作,故而E数组大小仍然为0,发生数组越界错误。
利用上述错误方式编写的测试程序如下:
package test_a;
class people{
int n;
Student s[]=new Student[n];
public people(int n) {
this.n=n;// TODO Auto-generated constructor stub
}
}
class Student{
int number;
}
public class Cdcs {
public static void main(String args[]) {
people p=new people(3);
System.out.println(p.s.length);
}
}
输出结果为0。即数组大小为零。
来源:http://www.cnblogs.com/inione/p/6731254.html
标签:java,数组越界,异常
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Hibernate使用SessionFactory创建Session案例详解
2022-03-04 06:21:28
android: targetSdkVersion升级中Only fullscreen activities can request orientation问题的解决方法
2023-07-26 13:56:08
springboot v2.0.3版本多数据源配置方法
2023-07-27 06:37:57
![](https://img.aspxhome.com/file/2023/5/57825_0s.png)
maven中配置项目的jdk版本无效的排查方式
2023-07-18 21:43:42
![](https://img.aspxhome.com/file/2023/9/57509_0s.png)
java实现简单的webservice方式
2023-11-25 03:59:58
![](https://img.aspxhome.com/file/2023/9/58989_0s.jpg)
android 6.0 写入SD卡的权限申请实例讲解
2023-07-27 03:12:37
java求100之内的素数(质数)简单示例
2023-11-07 03:12:32
JavaWeb开发之使用jQuery与Ajax实现动态联级菜单效果
2023-11-28 19:46:08
![](https://img.aspxhome.com/file/2023/6/60466_0s.png)
分布式系统下调用链追踪技术面试题
2023-11-25 05:55:00
![](https://img.aspxhome.com/file/2023/6/60126_0s.png)
Java class文件格式之特殊字符串_动力节点Java学院整理
2022-02-11 14:45:59
Java数据结构之链表、栈、队列、树的实现方法示例
2021-10-07 10:40:29
Android 显示GIF图片实例详解
2023-08-06 09:11:52
![](https://img.aspxhome.com/file/2023/8/86758_0s.jpg)
java使用字符画一个海绵宝宝
2023-09-08 09:45:19
![](https://img.aspxhome.com/file/2023/3/58453_0s.jpg)
Java创建子线程的两种方法
2023-11-24 07:00:05
Android实现CoverFlow效果控件的实例代码
2023-06-23 13:12:43
![](https://img.aspxhome.com/file/2023/5/83545_0s.png)
Spring Boot Admin实践详解
2023-08-25 06:57:53
SpringMVC执行步骤、Model的使用详解
2022-02-13 05:38:45
![](https://img.aspxhome.com/file/2023/2/62352_0s.png)
Java @Transactional指定回滚条件
2023-11-19 02:47:23
![](https://img.aspxhome.com/file/2023/7/59537_0s.png)
学习Java九大内置对象
2023-10-13 01:10:10
详解Java异常处理中finally子句的运用
2023-11-29 10:10:30