JAVA學習筆記(三)

來源:互聯網
上載者:User

標籤:大於   demo   筆記   []   類型   常見   font   4行   height   

數組

  數組是指一組資料的集合,數組中的每個資料被稱作元素。在數組中可以存放任意類型的元素,但同一個數組裡存放的元素類型必須一致。

一、數組的定義

  在Java中,可以使用以下格式來定義一個數組。如下

 資料類型[] 數組名 = new 資料類型[元素個數或數組長度];

  如何定義數組以及訪問數組中的元素

 1public class ArrayDemo01 { 2    public static void main(String[] args) { 3        int[] arr; // 聲明變數 4        arr = new int[3]; // 建立數組對象 5        System.out.println("arr[0]=" + arr[0]); // 訪問數組中的第一個元素 6        System.out.println("arr[1]=" + arr[1]); // 訪問數組中的第二個元素 7        System.out.println("arr[2]=" + arr[2]); // 訪問數組中的第三個元素 8        System.out.println("數組的長度是:" + arr.length); // 列印數組長度 9    } 10}

運行結果如下

二、數組的遍曆

  在運算元組時,經常需要依次訪問數組中的每個元素,這種操作稱作數組的遍曆。

public class ArrayDemo04 {    public static void main(String[] args) {        int[] arr = { 1, 2, 3, 4, 5 }; // 定義數組        // 使用for迴圈遍曆數組的元素        for (int i = 0; i < arr.length; i++) {            System.out.println(arr[i]); // 通過索引訪問元素        }    }}
 1.數組常見問題

  1.1數組最值

public class ArrayDemo05 {    public static void main(String[] args) {        int[] arr = { 4, 1, 6, 3, 9, 8 }; // 定義一個數組int max = arr[0]; // 定義變數max用於記住最大數,首先假設第一個元素為最大值        // 下面通過一個for迴圈遍曆數組中的元素        for (int x = 1; x < arr.length; x++) {if (arr[x] > max) { // 比較 arr[x]的值是否大於max                max = arr[x]; // 條件成立,將arr[x]的值賦給max            }        }        System.out.println("max=" + max); // 列印最大值    }}

  1.2 數組異常

  每個數組的索引都有一個範圍,即0~length-1。在訪問數組的元素時,索引不能超出這個範圍,否則程式會報錯。

 1public class ArrayDemo06 { 2    public static void main(String[] args) { 3        int[] arr = new int[4]; // 定義一個長度為4的數組 4        System.out.println("arr[0]=" + arr[4]); // 通過下標4訪問數組元素 5    } 6}

  運行結果中所提示的錯誤資訊是數組越界異常ArrayIndexOutOfBoundsException,出現這個異常的原因是數組的長度為4,其索引範圍為0~3,而上述代碼中的第4行代碼使用索引4來訪問元素時超出了數組的索引範圍。

2.二維數組

      多維陣列可以簡單地理解為在數組中嵌套數組,在程式中比較常見的就是二維數組。

  2.1 二維數組的定義方式

    第一種方式:

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

    第二種方式:

int[][] arr = new int[3][];//三行,列數不確定

    第三種方式:

int[][] arr = {{1,2},{3,4,5,6},{7,8,9}};//定義並賦值

  2.2 二維數組元素遍曆與數組元素累加和

class ArrayDemo09 {    public static void main(String[] args){        //一維數組的求累加和並遍曆        int[] arr = {10,20,30,40,50};        int sum = 0;         for (int i=0; i<arr.length; i++) {              //System.out.println(arr[i]);            sum += arr[i];}        System.out.println("sum= " + sum);        System.out.println("---------------------");        //二維數組的求累加和並遍曆        int[][] arr2 = { {1,2},{3,4,5},{6,7,8,9,10} };        int sum2 = 0;        for (int i=0; i<arr2.length; i++) {            for (int j=0; j<arr2[i].length; j++) {                 //System.out.println(arr2[i][j])                sum2 += arr2[i][j];            }        }        System.out.println("sum2= "+ sum2);    }}

 

      

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.