<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

    Java語言基礎:基本數組類型

    發布時間:2011-3-29 20:01    發布者:1770309616
    關鍵詞: Java語言基礎 , 基本數組類型
    Java語言基礎.pdf (205.39 KB)


    1.  一維數組的聲明和初始化,分成動態和靜態:view plaincopy to clipboardprint?
    01.// 動態創建10個整型,默認初始化為0   
    02.int[] a1 = new int[10];   
    03.   
    04.// 靜態創建4個整型,并初始化為括號中的值   
    05.int[] a2 = {1, 2, 3, 4};   
    06.   
    07.// 動態創建MyClass數組,需要遍歷創建數組中的每個類實例   
    08.MyClass[] a3 = new MyClass[3];   
    09.for (int i = 0; i < a3.length; ++i)   
    10.    MyClass = new MyClass();   
    11.   
    12.// 靜態創建MyClass數組,用括號中的實例初始化數組   
    13.MyClass[] a4 = {new MyClass(), new MyClass(), new MyClass};  
    // 動態創建10個整型,默認初始化為0
    int[] a1 = new int[10];

    // 靜態創建4個整型,并初始化為括號中的值
    int[] a2 = {1, 2, 3, 4};

    // 動態創建MyClass數組,需要遍歷創建數組中的每個類實例
    MyClass[] a3 = new MyClass[3];
    for (int i = 0; i < a3.length; ++i)
        MyClass = new MyClass();

    // 靜態創建MyClass數組,用括號中的實例初始化數組
    MyClass[] a4 = {new MyClass(), new MyClass(), new MyClass};
    2.  多維數組可理解為數組的數組,同樣可以有兩種聲明形式:view plaincopy to clipboardprint?
    01.public class Main {           
    02.    static void printInt(int[][] ints) {   
    03.        for (int i = 0; i < ints.length; ++i)   
    04.            for (int j = 0; j < ints.length; ++j)   
    05.            System.out.println(ints[j]);   
    06.    }   
    07.   
    08.    public static void main(String[] args) {   
    09.        // 動態創建二維數組,并遍歷初始化之   
    10.        int[][] ints = new int[2][2];   
    11.        for (int i = 0; i < ints.length; ++i)   
    12.            for (int j = 0; j < ints.length; ++j)   
    13.                ints[j] = i + j;   
    14.        printInt(ints);   
    15.   
    16.        // 靜態創建二維數組   
    17.        int[][] ints2 = {{1, 2}, {3, 4}};   
    18.        printInt(ints2);   
    19.    }   
    20.}  
    public class Main {        
        static void printInt(int[][] ints) {
            for (int i = 0; i < ints.length; ++i)
                for (int j = 0; j < ints.length; ++j)
                System.out.println(ints[j]);
        }

        public static void main(String[] args) {
            // 動態創建二維數組,并遍歷初始化之
            int[][] ints = new int[2][2];
            for (int i = 0; i < ints.length; ++i)
                for (int j = 0; j < ints.length; ++j)
                    ints[j] = i + j;
            printInt(ints);

            // 靜態創建二維數組
            int[][] ints2 = {{1, 2}, {3, 4}};
            printInt(ints2);
        }
    } 3.  可變數組的聲明形式:view plaincopy to clipboardprint?
    01.public class Main {           
    02.    static void printInt(int[][] ints) {   
    03.        for (int i = 0; i < ints.length; ++i)   
    04.        {   
    05.            for (int j = 0; j < ints.length; ++j)   
    06.                System.out.print(ints[j] + " ");   
    07.            System.out.println();   
    08.        }   
    09.    }   
    10.   
    11.    public static void main(String[] args) {   
    12.        // 動態創建二維可變數組,先創建第一維   
    13.        int[][] ints = new int[2][];   
    14.        // 再確定第二維的元素數   
    15.        for (int i = 0; i < ints.length; ++i)   
    16.            ints = new int[i + 3];   
    17.        printInt(ints);   
    18.   
    19.        // 靜態創建二維可變數組   
    20.        int[][] ints2 = {{1, 2}, {3, 4, 5}};   
    21.        printInt(ints2);   
    22.    }   
    23.}   
    24.// 輸出為:   
    25.0 0 0   
    26.0 0 0 0   
    27.1 2   
    28.3 4 5
    本文地址:http://www.portaltwn.com/thread-60390-1-1.html     【打印本頁】

    本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
    152x 發表于 2015-5-22 01:24:12
    感謝分享
    您需要登錄后才可以發表評論 登錄 | 立即注冊

    廠商推薦

    • Microchip視頻專區
    • EtherCAT®和Microchip LAN925x從站控制器介紹培訓教程
    • MPLAB®模擬設計器——在線電源解決方案,加速設計
    • 讓您的模擬設計靈感,化為觸手可及的現實
    • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
    • 貿澤電子(Mouser)專區
    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷