技术标签: jvm python java 多线程 编程语言
java.lang.ArrayIndexOutOfBoundsException is a runtime exception, so it’s a unchecked exception and don’t need to be thrown explicitly from method. Refer Exception Handling in java
java.lang.ArrayIndexOutOfBoundsException是运行时异常,因此它是未经检查的异常,不需要从方法中显式抛出。 请参阅Java中的异常处理
ArrayIndexOutOfBoundsException super classes are Exception
, RuntimeException
and IndexOutOfBoundsException
.
ArrayIndexOutOfBoundsException超类是Exception
, RuntimeException
和IndexOutOfBoundsException
。
Let’s look at a simple example where our program may throw ArrayIndexOutOfBoundsException based on user input.
让我们看一个简单的示例,其中我们的程序可能根据用户输入抛出ArrayIndexOutOfBoundsException。
package com.journaldev.exceptions;
import java.util.Scanner;
public class ArrayIndexOutOfBoundsExceptionExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of int array:");
int size = sc.nextInt();
int[] intArray = new int[size];
for (int i = 0; i < size; i++) {
System.out.println("Please enter int value at index " + i + ":");
intArray[i] = sc.nextInt();
}
System.out.println("Enter array index to get the value:");
int index = sc.nextInt();
sc.close();
System.out.println("Value at " + index + " = " + intArray[index]);
}
}
Below log shows one of the execution of above program.
下面的日志显示了以上程序的执行之一。
Enter size of int array:
3
Please enter int value at index 0:
1
Please enter int value at index 1:
2
Please enter int value at index 2:
3
Enter array index to get the value:
4
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.journaldev.exceptions.ArrayIndexOutOfBoundsExceptionExample.main(ArrayIndexOutOfBoundsExceptionExample.java:23)
So our program can throw ArrayIndexOutOfBoundsException because of illegal input from user. Also notice that exception stack trace prints the illegal index causing the exception.
因此,由于用户的非法输入,我们的程序可能会抛出ArrayIndexOutOfBoundsException。 还要注意,异常堆栈跟踪会打印出导致异常的非法索引。
We shouldn’t try to recover from this exception, we should try to mitigate it by checking if the index value passed is a valid value or not. Also note that this situation can occur mostly in case of user input, if we are creating array ourself and iterating over it’s elements then chances of this exception are less.
我们不应该尝试从此异常中恢复,而应尝试通过检查传递的索引值是否为有效值来减轻这种异常。 还要注意的是,这种情况多半发生在用户输入的情况下,如果我们自己创建数组并对其元素进行迭代,则发生此异常的机会就会减少。
Below code snippet shows the small change in program while taking user input to access the element, if value passed is invalid then a warning message is shown to pass the valid value.
下面的代码片段显示了在使用用户输入来访问元素时程序中的微小变化,如果传递的值无效,则会显示警告消息以传递有效的值。
boolean exit = false;
while (!exit) {
System.out.println("Enter array index to get the value:");
int index = sc.nextInt();
if (index < 0 || index >= size) {
System.out.println("Valid index range is from 0 to " + (size - 1));
} else {
System.out.println("Value at " + index + " = " + intArray[index]);
exit = true; //to terminate the program
sc.close(); //close resources
}
}
Now the output will be like below when illegal index is passed.
现在,通过非法索引后,输出将如下所示。
Enter size of int array:
3
Please enter int value at index 0:
1
Please enter int value at index 1:
2
Please enter int value at index 2:
3
Enter array index to get the value:
4
Valid index range is from 0 to 2
Enter array index to get the value:
-1
Valid index range is from 0 to 2
Enter array index to get the value:
2
Value at 2 = 3
So this is how we avoid getting ArrayIndexOutOfBoundsException in our program. That’s all for a quick roundup on java.lang.ArrayIndexOutOfBoundsException
.
因此,这就是我们避免在程序中获取ArrayIndexOutOfBoundsException的方法。 这就是对java.lang.ArrayIndexOutOfBoundsException
的快速总结。
Reference: API Doc
参考: API文档
翻译自: https://www.journaldev.com/18092/java-lang-arrayindexoutofboundsexception
文章浏览阅读834次。过去一段时间,在工业控制领域形成有两个竞争阵线,有些说PC能很好地用于控制,而有些人说PC永远不适合用于工厂的环境。今天,这些竞争对手很容易接受PC机用于许多工业生产控制的现状,因为一个接一个的制造商已经在部分生产中采用了PC控制方案。因此,传统上坚持PC是办公机器,不适合工厂灰尘环境的观点也会发生转变。 Beckhoff 是基于 PC 的自动化技术的先驱者之一:早在 1986 年,Beckhoff 的第一个 PC 控制系统就已问世。如今,Beckhoff 已将积累多年的专有技术应用到工业 PC 中。B_基于pc的自动控制
文章浏览阅读1.6k次。1、MSELoss原理MSELoss计算预测值和真实值的mean squared error (squared L2 norm,均方误差) 。若是mean:先计算预测值x与真实值y这两个tensor中的对应位置的两个元素的差的平方,得到一个新的同样大小的tensor,然后求这个tensor中所有元素的均值;若是sum:先计算预测值x与真实值y这两个tensor中的对应位置的两..._pytorch中.mse_loss具体原理
文章浏览阅读2.9k次,点赞5次,收藏9次。今天我们来说道说道微信小程序里面当中的几种跳转方式!微信小程序跳转的方式总共有5种,可以对应各种的应用场景。1.wx.navigateTo()保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。可封装函数为://跳转新页面页面,保留当前页面。function navigateTo(url) { wx.navigateTo({ ur..._若依小程序在哪封装的this.$tab.navigateto
文章浏览阅读1.8w次,点赞59次,收藏338次。JUnit详解什么是JUnitJUint是Java编程语言的单元测试框架,用于编写和运行可重复的自动化测试。JUnit 促进了“先测试后编码”TDD的理念,强调建立测试数据的一段代码,可以先测试,然后再应用。这个方法就好比“测试一点,编码一点,测试一点,编码一点……”,增加了程序员的产量和程序的稳定性,可以减少程序员的压力和花费在排错上的时间。TDD 是Test-Driven Development(测试驱动开发)的缩写。JUnit的特点开源框架提供注解来识别测试方法提供断言来测试预期结果_junit
文章浏览阅读518次。钟控收音机选购指南 钟控收音机的品牌选择: 现在能在国内购买到的钟控收音机基本上以SONY公司产品为主,这当中,间或有一些松下,EMERSON(爱默生),RCA(美国无线)GE(通用)TIMEX,飞利浦产品出售。还有一些少量的国产品牌也有在市场上销售 在产品质量上,在这些品牌当中,个人认为只有SONY和松下公司的产品最好,特别是SONY公司,不但产品型号众多,..._数调钟控收音机常用的lcd显示芯片
文章浏览阅读386次。//暴力求解 O(n*n) O(1)class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { for(int i=0; i<nums.size()-1; i++){ for(int j=i+1; j<nums.size(); j++){ if(nums[i]+nums[j]==target) return vector<int>_c++力扣算法第一题
文章浏览阅读4.5k次,点赞3次,收藏5次。其实很简单,QT有现成的库来处理这个操作,那就是 int QString::toInt(bool *ok, int base), 只是用的时候需要注意 base 默认为 10, 也就是它认为被转化的字符串是十进制,需要显示的对定位十六进制; QString str = "0xf8"; ..._qt 16进制字符串转int
文章浏览阅读5.8k次。在向数据库中插入数据时,如果插入过于频繁的话,我们都知道使用批量插入,也即是使用addBatch方法。那么如果一次查询操作返回的数据量很大,JDBC的方式是一次返回部分数据,就像是流处理那样;还是一次返回全部的数据?经过简单的调研,发现mysql 和 postgresql都是默认返回全量数据,一些分布式数据库默认返回部分数据,比如说默认返回100条数据,当对这部分数据操作完成后再去数据拉取..._数据库批量查询
文章浏览阅读1.7w次,点赞41次,收藏255次。不同图表的数据获取有一定的区别在这些区别上花了不少功夫试验,把最后成功的方法做个记录,如果有类似项目要做的话,也可看看当个参考。后端后端都大同小异,方法上没有区别,在这里以柱状图为例。sql:数据库的表格先写好,名称、类型、数据,然后连接数据库用的是Navicat写,表名:sys_mapbar在IDEA里,写XML文件,与数据库调用直接相关的语句<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper _echarts后端怎么向前端传数据
文章浏览阅读7.1k次,点赞22次,收藏9次。今天是 2021 年 9 月 4 日,在历史上的今天,柯达相机获得专利,从此成立相机帝国;人工智能之父 John McCarthy 出生;Google 公司正式成立,改变了许多人的命运。_9月4日历史上的今天 科技时间
文章浏览阅读204次。1024程序员节是广大程序员的共同节日。1024是2的十次方,二进制计数的基本计量单位之一。针对程序员经常周末加班与工作日熬夜的情况,部分互联网机构倡议每年的10月24日为1024程序员节,在这一天建议程序员拒绝加班。 程序员就像是一个个1024,以最低调、踏实、核心的功能模块搭建起这个科技世界。1G=1024M,而1G与1级谐音,也有一级棒的意思。 程序员(英文Programmer)是从事前端、后端程序开发、系统运维、测试等的专业人员。一般将程序员分为程序设计人员和程序编码人员,但两..._1024_hello_world
文章浏览阅读1k次。图像处理与VTK/ITK文章列表 #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <iostream> #include <sstream> #include <string>#i..._dcmimage.h