Java ArrayIndexOutOfBoundsException是什么意思?如何消除它?以下是触发异常的代码示例:
我一直在用java编写一些代码,我只是遇到了一个错误,我不知道为什么,因为我只是运行了一些类似这样的代码,它工作得很好Java ArrayIndexOutOfBoundsException是什么意思?如何消除它?以下是触发异常的代码示例:,java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,我一直在用java编写一些代码,我只是遇到了一个错误,我不知道为什么,因为我只是运行了一些类似这样的代码,它工作得很好 String [] month = {"Jan", "Feb", "Mar", "Apr", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; double [] temperature ={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1,
String [] month = {"Jan", "Feb", "Mar", "Apr", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
double [] temperature ={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double [] precipitation ={3.5, 3.4, 4.3, 2.9, 3.2, 6.8, 6.1, 6.6, 4.4, 2.5, 2.2, 2.6};
for( int index = 0; index < temperature.length; index++)
{
System.out.print(""+month [index] +". "+temperature [index]+" "+precipitation [index]);
System.out.println();
}
String[]月={“一月”、“二月”、“三月”、“四月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
双[]温度={54.3,57.0,62.5,67.6,74.3,79.2,80.9,80.4,77.8,70.1,62.8,56.3};
双[]降水量={3.5,3.4,4.3,2.9,3.2,6.8,6.1,6.6,4.4,2.5,2.2,2.6};
对于(int index=0;index
通过检查,我可以看到您的月
数组缺少五月的月份,因此其中只有11个元素。因此,当您迭代temperature
数组中的12个元素时,您将在最终温度期间得到一个数组越界异常,因为在该位置的month
中没有相应的enter
要解决此问题,请确保月份
和温度
的大小相同:
String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec" };
May
我提醒你一件事?那是什么?……没关系,我刚拿到它,可能是重复的,不是的,那篇帖子启发我问这个问题,因为我有同样的问题,只是出于不同的原因。啊,谢谢,我没听清楚。