# 數組聲明:
```
int scores[] =new int[10]; //聲明一個數組
```
## 聲明一個10個下標的數組
# 數組遍歷:
## 普通for循環遍歷
```
int scores[] =new int[10]; //聲明一個數組
for(int a=0;a<10;a++){
System.out.println(scores[a]);
//遍歷循環
}
```
## 增強for循環:
## for(int a: 數組名稱)
```
int scores[] =new int[10]; //聲明一個數組
for(int a:scores){
//增強型寫法
System.out.println(scores[a]);
//遍歷循環
}
```
## 增強for循環優缺點:
增強for循環:
優點:代碼簡單
缺點:單純的增強for循環不能涉及跟索引相關的操作
# 代碼案例:
```
import java.util.Scanner;
public class TestArray03{
public static void main(String[] args){
//功能:鍵盤錄入十個學生的成績,求和,求平均數:
//定義一個int類型的數組,長度為10 :
int[] scores = new int[10];
//定義一個求和的變量:
int sum = 0;
Scanner sc = new Scanner(System.in);
for(int i=1;i<=10;i++){//i:控制循環次數
System.out.print("請錄入第"+i+"個學生的成績:");
int score = sc.nextInt();
scores[i-1] = score;
sum += score;
}
System.out.println("十個學生的成績之和為:"+sum);
System.out.println("十個學生的成績平均數為:"+sum/10);
//求第6個學生的成績:
//System.out.println(scores[5]);
/*
System.out.println(scores[0]);
System.out.println(scores[1]);
System.out.println(scores[2]);
System.out.println(scores[3]);
//....
System.out.println(scores[9]);
*/
//將數組中的每個元素進行查看--》數組的遍歷:
//方式1:普通for循環---》正向遍歷:
for(int i=0;i<=9;i++){
System.out.println("第"+(i+1)+"個學生的成績為:"+scores[i]);
}
//方式2:增強for循環:
//對scores數組進行遍歷,遍歷出來每個元素都用int類型的num接收:
int count = 0;
for(int num:scores){
count++;
//每次都將num在控制臺輸出
System.out.println("第"+count+"個學生的成績為:"+num);
}
/*
增強for循環:
優點:代碼簡單
缺點:單純的增強for循環不能涉及跟索引相關的操作
*/
//方式3:利用普通for循環: 逆向遍歷:
for(int i=9;i>=0;i--){
System.out.println("第"+(i+1)+"個學生的成績為:"+scores[i]);
}
}
}
```