集册 Java实例教程 数组边界检查

数组边界检查

欢马劈雪     最近更新时间:2020-01-02 10:19:05

448
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
数组边界检查
/*来自 
 N o w J a v a . c o m - 时  代  Java*/

public class Main {

  public static void main(String[] args) {

    int[] test = new int[3];

      

    System.out.println("Assigning 12 to the first element");

    test[0] = 12;  // index 0 is between 0 and 2. Ok


    System.out.println("Assigning 9 to the fourth element");

    

    // index 3 is not between 0 and 2. At runtime, an exception is thrown.

    test[3] = 9; 

    
    /**
    来 自 n o w j a v a . c o m
    **/

    System.out.println("We will not get here");    

  }

}