zoukankan      html  css  js  c++  java
  • [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
        }
    }
    
    赞赏-支付宝二维码
    本文作者千千寰宇
    本文链接 https://www.cnblogs.com/johnnyzen
    关于博文:评论和私信会在第一时间回复,或直接私信我。
    版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
    声援博主:若本文对您有帮助,可点击右下角推荐一下。您的鼓励、【赞赏】(左侧赞赏支付码)是博主技术写作的重要动力!
  • 相关阅读:
    如何使用NuGet package .nupkg文件?
    利用Z.Expressions.Eval表达式求值
    表达式实现填写“阶段标记”属性
    NX导入DWG失败
    简单工厂模式(Simple Factory Pattern)
    电脑装系统蓝屏未解决
    idea intellij 配置总结
    poiexcel 笔记
    Springboot
    Springboot--maven命令子模块单独打包
  • 原文地址:https://www.cnblogs.com/johnnyzen/p/15543983.html
Copyright © 2011-2022 走看看