Java基礎-數組

來源:互聯網
上載者:User

標籤:random   rand   cores   mat   二維   類型   java   foreach   遍曆   

數組

聲明
資料類型[] 數組名;
或 資料類型 數組名[]
int[] scores; int scores[];
分配空間
數組名= new 資料類型[數組長度];
scores=new int[5];
聲明和分配空間一起
int[] scores=new int[5];
賦值
scores[0]= 1;..
聲明 分配空間 賦值一起
int[] scores={1,2,3,4}
或 int[] scores=new int[]{1,2,3,4}
4)foreach 運算元組
For(元素類型 元素變數 :遍曆對象){ }
例 for(String score : scores){}
5)nums[i]=(int)(Math.random()*100)
隨機產生100以內的整數元素

二維數組

1)聲明並分配空間
資料類型[][] 數組名= new 資料類型[行的個數][列的個數]
Int[][] num=new int[2][3];
2)聲明的同時為其賦值
資料類型[][] 數組名={{1,2,3},{1,2,3},{1,2,3}}
3)二維數組的二重迴圈輸出
For(int i=0; i < num.length;i++){
For(int j=0;j < num[i].length;j++){}
}

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.