国产精品免费无遮挡无码永久视频-国产高潮视频在线观看-精品久久国产字幕高潮-国产精品99精品无码视亚

Java語言基礎(chǔ):基本數(shù)組類型

發(fā)布時(shí)間:2011-3-29 20:01    發(fā)布者:1770309616
Java語言基礎(chǔ).pdf (205.39 KB)


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

// 靜態(tài)創(chuàng)建4個(gè)整型,并初始化為括號(hào)中的值
int[] a2 = {1, 2, 3, 4};

// 動(dòng)態(tài)創(chuàng)建MyClass數(shù)組,需要遍歷創(chuàng)建數(shù)組中的每個(gè)類實(shí)例
MyClass[] a3 = new MyClass[3];
for (int i = 0; i < a3.length; ++i)
    MyClass = new MyClass();

// 靜態(tài)創(chuàng)建MyClass數(shù)組,用括號(hào)中的實(shí)例初始化數(shù)組
MyClass[] a4 = {new MyClass(), new MyClass(), new MyClass};
2.  多維數(shù)組可理解為數(shù)組的數(shù)組,同樣可以有兩種聲明形式: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.        // 動(dòng)態(tài)創(chuàng)建二維數(shù)組,并遍歷初始化之   
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.        // 靜態(tài)創(chuàng)建二維數(shù)組   
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) {
        // 動(dòng)態(tài)創(chuàng)建二維數(shù)組,并遍歷初始化之
        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);

        // 靜態(tài)創(chuàng)建二維數(shù)組
        int[][] ints2 = {{1, 2}, {3, 4}};
        printInt(ints2);
    }
} 3.  可變數(shù)組的聲明形式: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.        // 動(dòng)態(tài)創(chuàng)建二維可變數(shù)組,先創(chuàng)建第一維   
13.        int[][] ints = new int[2][];   
14.        // 再確定第二維的元素?cái)?shù)   
15.        for (int i = 0; i < ints.length; ++i)   
16.            ints = new int[i + 3];   
17.        printInt(ints);   
18.   
19.        // 靜態(tài)創(chuàng)建二維可變數(shù)組   
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.4huy16.com/thread-60390-1-1.html     【打印本頁】

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

廠商推薦

  • Microchip視頻專區(qū)
  • “芯”光璀璨,鵬城共賞——2025 Microchip中國技術(shù)精英年會(huì)深圳站回顧
  • Microchip第22屆中國技術(shù)精英年會(huì)上海首站開幕
  • Microchip第22屆中國技術(shù)精英年會(huì)——采訪篇
  • 電動(dòng)兩輪車設(shè)計(jì)生態(tài)系統(tǒng)
  • 貿(mào)澤電子(Mouser)專區(qū)
關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權(quán)所有   京ICP備16069177號(hào) | 京公網(wǎng)安備11010502021702
快速回復(fù) 返回頂部 返回列表