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
    }
}

相关文章:

  • 2021-04-27
  • 2021-09-27
  • 2021-12-28
  • 2021-09-05
  • 2021-12-04
  • 2021-04-17
  • 2021-12-16
  • 2022-12-23
猜你喜欢
  • 2021-11-04
  • 2022-12-23
  • 2021-06-20
  • 2022-12-23
  • 2021-10-18
  • 2021-11-14
  • 2020-03-30
相关资源
相似解决方案