我对Java很陌生。我试图在此多维数组的末尾添加一个名称。

    String[][] cartoons = new String [][] {
    { "Flintstones", "Fred", "Wilma", "Pebbles", "Dino" },
    { "Rubbles", "Barney", "Betty", "Bam Bam" },
    { "Jetsons", "George", "Jane", "Elroy", "Judy", "Rosie", "Astro" },
    { "Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma", "Fred", "Daphne" } };


  cartoons[0][5] = "VELMA";

  System.out.println(cartoons[0][5]);


如您所见,它已超出范围。

run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at javaapplication2.JavaApplication2.main(JavaApplication2.java:46)
Java Result: 1


我在这里做错了什么?

最佳答案

您在cartoons[0]中有5个元素,这意味着从cartoons[0][0]cartoons[0][4]的访问索引

和这里


  cartoons [0] [5] =“ VELMA”;


您想访问实际上超出范围的第6个索引

这就是为什么你越来越


  线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:5
      在javaapplication2.JavaApplication2.main(JavaApplication2.java:46)

10-08 08:33