java.lang.IndexOutOfBoundsException:インデックス:0、サイズ:0



Java Lang Indexoutofboundsexception



public class Test{ public static void main(String[] args){ List<String> list = new ArrayList<String>() System.out.println(list.get(0)) } }

プログラム実行後のエラーメッセージは次のとおりです。

Exception in thread 'main' java.lang.IndexOutOfBoundsException: Index:0, Size:0 at java.util.ArrayList.rangCheck(ArrayList.java:433) at java.util.ArrayLIst.get(ArrayList.java:433) at Test.main(Test.java:14)

エラーメッセージ分析:
エラーメッセージat java.util.ArrayList.rangCheck(ArrayList.java:433)から、ArrayList.javaの433行目にエラーが報告されました。次に、ソースコードを確認します。



private void rangeCheck(int index){ if(index >= size) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)) }

index = 0なので、size = 0です。したがって、IndexOutOfBoundsExceptionをスローします