整理Java基礎知識--數組1

來源:互聯網
上載者:User

標籤:c/c++   必須   函數   使用   var   4.4   引用   ++i   void   

Java 數組
數組是用來儲存固定大小的同類型的元素
聲明
數組必須先聲明 才能使用
datatype[] arrayRefVar;//首選 []放在最後是為了C/C++的快速理解Java
char[] ch ;或者 char ch[];
char ch = new char[arraySize];
1.建立一個char數組 含有 arraySize元素
2.把建立 char數組的引用 賦值給變數ch
數組的使用

public class TestArr{    public static void main(String[] args){        double[] arr = {1.1,2.2,4.4,3.3};        for(double x:arr){            System.out.println(x);                  }//列印所有數組元素        double total = 0;        for(int i = 0;i < arr.length;++i){            total = total + arr[i];                     }        System.out.println("Total = " + total);        //計算並列印所有元素的和        double max = arr[0];        for(int i = 1;i < arr.length;++i ){            if (arr[i] > max) max = arr[i];        }        System.out.println("Max = " + max);    }   //尋找並列印最大元素值}輸出結果:1.12.24.43.3Total = 11.0Max = 4.4

數組作為函數的參數:

class A{    static void printArray(int[] array) {        for (int i = 0; i < array.length; i++) {            System.out.print(array[i] + " ");        }    }}public class TestA{  public static void main(String args[]){    int[] arr = {1, 2, 3, 4, 5, 6};    A.printArray(arr);  }}輸出結果:1 2 3 4 5 6

整理Java基礎知識--數組1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.