java中的數組

來源:互聯網
上載者:User

標籤:mat   分享   技術分享   and   技術   完成   數組初始化   類型   素數   

一)數組:1)數組的長度和類型固定2)幾大要素:int[] arr = new int[5];下標(數組的下標從0開始)元素:arr[i] i>=0&&i<5類型,此處為int型長度,查看數組長度arr.length,此處為5  二)數組中兩大基本概念:棧-堆1、本質區別:數組中的棧不會做預設的初始化:例:int n; //將會報錯int n = 0; //只有將變數初始化之後系統才會通過編譯數組中的堆進行預設的初始化:
int 0
float 0.0
char Null 字元
boolean false
非基礎資料型別 (Elementary Data Type) null
 2、具體表現:局部變數都儲存在棧中  3、數組初始化:1)int[] arr = new int[5];arr[0] = 1;arr[1] = 2;……2)int[] score = new int[]{80,60,90}//建立數組與賦值可以在同一條語句中完成int[] score = {80,60,90}3)for(int i = 0; i < arr.length; i++){arr[i] = (int)(Math.random()*10);//此處調用Math類中的random方法,並且進行了類型轉換} 4、二維數組:二維數組本質上還是一維數組,僅僅是多了一個維度;可以將二維數組理解為多個一維數組組成的一個新的數組。1)表達:int[][] sc = new int[4][5];則sc.length == 4;表示此二維數組中共含有4個一維數組,即該二維數組的長度為4 for(int i = 0 ; i < sc.length; i ++){int[] tep = sc[i];}則tep.length == 5;此處表示該二維數組中的每個一維數組對應的元素個數為5 2)儲存:int[][] s = new int[3][2];例:int[][] arr = {{1,2,3},{4,5},{6,7,8,9}};則: 棧只會儲存數組在儲存空間中的位置,並不會儲存具體資料內容,數組的 元素資料存放區在堆中 5、數組的操作:排序:冒泡排序:double[] arr = new double[5];for(int i = 0; i< arr.length; i++) {  arr[i] = sc.nextDouble();} for(int i = 0; i < arr.length -1; i++) {  for(int j = 0; j< arr.length-2 ; j++) {    if(arr[j] > arr[j+1]) {    double temp = arr[j];    arr[j] = arr[j+1];    arr[j + 1] = temp;    }  }}  

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.