[Java SE]数组超界异常分析(IndexOutOfBoundsException/ArrayIndexOutOfBoundsException)
import org.junit.Test;
import java.util.ArrayList;
/**
* @author: Johnny
* @date: 2021/11/12 11:17:24
* @description: 测试数组超界异常 IndexOutOfBoundsException ArrayIndexOutOfBoundsException
* https://blog.csdn.net/be_happy_mr_li/article/details/53302411
*/
public class ArrayExceptionTest {
@Test
public void test004() {
ArrayList<String> array = new ArrayList<String>();
array.add(0,"hello world");
array.add(1,"hello world");
int index = array.indexOf("22"); //-1
System.out.println(index);
//array.set(index,"hello world"); //java.lang.ArrayIndexOutOfBoundsException: -1
array.add(index,"hello world");//java.lang.IndexOutOfBoundsException: Index: -1, Size: 2
}
@Test
public void test003() {
ArrayList<String> array = new ArrayList<String>();
array.set(0,"hello world");//java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
}
@Test
public void test002() {
ArrayList<String> array = new ArrayList<String>();
//array.add(0,"hello world");//正常
array.add(1,"hello world");//java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
}
}
![QQ沟通交流群](https://blog-static.cnblogs.com/files/johnnyzen/cnblogs-qq-group-qrcode.gif?t=1679679148)
本文作者:
千千寰宇
本文链接: https://www.cnblogs.com/johnnyzen/p/15543983.html
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!
本文链接: https://www.cnblogs.com/johnnyzen/p/15543983.html
关于博文:评论和私信会在第一时间回复,或直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
日常交流:大数据与软件开发-QQ交流群: 774386015 【入群二维码】参见左下角。您的支持、鼓励是博主技术写作的重要动力!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步