【Java】一維數組

來源:互聯網
上載者:User

標籤:方法   linear   越界   初始化   thread   常量   https   []   nbsp   

/*1.示範初始化一維數組的三種方法2.示範數組的 length 屬性,用 length 擷取數組長度*/public class linear_array {    public static void main(String args[]) {        int arr1[] = new int[6];        arr1[0]=1;        double arr2[] = new double[]{9.0,5.5,3.6,6.9,13.4};                int arr3[] = {0,2,3,4,2,00,8};        System.out.println(arr1 [0]);//Output:1        System.out.println(arr2 [1]);//Output:5.5        System.out.println(arr3 [2]);//Output:3        //System.out.println(arr3 [9]);        /*        Output:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9               at linear_array.main(linear_array.java:16)               由於下標越界,導致拋出數組下標溢出異常        */        //數組的下標從 0 開始,數組最大下標為 數組長度 - 1        int arr4[] = new int[9];        System.out.println("數組 arr4 的長度為:" + arr4.length);//Output:9        int arr5[] = new int[4+3];        System.out.println("數組 arr5 的長度為:" + arr5.length);//Output:7        /*        數組的長度只能用 ≧0 的整數表示,一般都是 >0 的整數,等於 0 時無意義        數組的長度可以用一個運算式表示        length 的值是個常量        */    }} 

 :https://gitee.com/sxyin/codes/7n3lji02udgkyvxt8r5ao79

【Java】一維數組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.