Java數組筆記

來源:互聯網
上載者:User

標籤:整數   print   java   ati   cpu   short   組元   二維數組   引用   

# 數組--------記憶體 #

2017/8/31 15:29:19 

## 數組(容器) ##
- 為了儲存同種資料類型的多個值
- 概念
    - 數組是可以儲存同一種資料類型多個元素的集合,也可以看成是一個容器
    - 數組既可以儲存基礎資料型別 (Elementary Data Type),也可以儲存引用資料類型
- 格式
    - 資料類型[] 數組名 = new 資料類型[數組的長度];
    -    int[]  arr = new int[5];
    - 資料類型[] 數組名 = new 資料類型[]{元素1,元素2,元素3,...};
    -    int[] arr = new int[]{11,22,33,44,55};---------數組長度為5
- 數組初始化
    - 為數組開闢連續的記憶體空間,並為每個數組元素賦予值
        - 動態初始化:只指定長度,由系統決定初始化值
            -   int[] arr = new int[5];
            -  
            1. 整數類型:byte,short,int,long,預設初始化值都是0
            2. 浮點類型:float,double,預設初始化值為0.0
            3. 布爾類型:Boolean預設初始化值為false
            4. 字元類型:char初始化值為‘\u0000’    -------char在記憶體中佔兩個位元組,是16個二進位位,    \u0000----每一個0代表十六進位的0,四個0就是十六個二進位位
        - 靜態初始化:給出初始化值,由系統決定長度
            -  資料類型[] 數組名 = new 資料類型[]{元素1,元素2,...};
            - ` int[] arr = new int[]{11,22,33,44,55};`  先聲明,後賦值
            -  `int[] arr = {11, 22,33,44,55}; ` 聲明和賦值在一行
   
                    ` int[] arr;

                    arr = new int[]{11,22,33,44,55};
            
                    int[] arr2;
                    arr2 = {11,22,33,44,55}    //會報錯,,,聲明和賦值必須在一行
                    `
            
## 棧 ##
- 儲存局部變數---
    - 局部變數:定義在方法聲明上和方法中的變數
        - int x = 10;
## 堆 ##
- 儲存new出來的數組或對象(新建立)
## 方法區 ##
-物件導向-
## 本地方法區 ##
-和系統相關
## 寄存器 ##
- 給CPU使用(.class)

## 一個數組的記憶體配置圖解 ##
![](http://i.imgur.com/HCpk6gu.png)

# 二維數組 #

    `        int[][] arr = new int[3][2];                        //定義二維數組
        System.out.println(arr);            //二維數組                                    [[[email protected]
        System.out.println(arr[0]);            //二維數組中第一個一維數組                    [[email protected]
        System.out.println(arr[0][0]);        //二維數組中第一個一維數組的第一個元素        0

    
    /*
    //int[][] arr = new int[3][2];    
    //以下兩種也是表示數組的格式
        int arr[][] = new int[][];
        int[] arr[] = new int[][];

        int x;
        int y;
        int x,y;
        
        int[] x;
        int[] y[];

        int[] x,y[];                //x是一維數組,y是二維數組
        */`

**int[][] arr = new int[3][];**

----------二維數組中有三個一維數組

賦值::
        
        `arr[0] = new int[3];
        arr[1]    = new int[5];
        `
##二維數組遍曆##
外迴圈控制二維數組的長度,其實就是一維數組的個數;

內迴圈控制一維數組的長度。


    `class Demo2_Array {
        public static void main(String[] args) {
            int[][] arr = {{1,2,3},{4,5,6},{4,3}};
            System.out.println(arr);
            System.out.println(arr[0]);
            System.out.println(arr[0][0]);

            bianli(arr);    
        }
        public static void bianli(int[][] arr){
            for (int i = 0; i < arr.length ;i++ ){            //i代表每一個二維數組中的一維數組
                for (int j = 0;j < arr[i].length ;j++ ){
                    System.out.print(arr[i][j] + " ");
                }    
                System.out.println();
            }
        }
     }`


# java中的參數傳遞 #

1. 基礎資料型別 (Elementary Data Type)的值傳遞,不改變原值,因為調用後就會彈出棧,局部變數隨之消失。
2. 引用資料類型的值傳遞,改變原值,因為即使方法彈出棧,但是堆記憶體數組的對象還在,可以通過地址繼續訪問。

*兩種說法:

1、Java中既是傳值,也是傳址,基礎資料型別 (Elementary Data Type)傳遞的值,引用資料類型傳遞的地址

2、Java中只有傳值,因為地址值也是值*

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.