一、本章目標
掌握數組的基本用法
掌握數組的幾種典型應用
二、內容
1 數組概述
1.1 為什么需要數組
-
問題:Java考試結束后,老師給張浩分配了一項任務,讓他計算全班(30人)的平均分
解決方案
缺點
變量太多
不利於數據處理
1.2 Java中的數組
什么是數組
數組是一個變量,存儲相同數據類型的一組數據
數組與變量的區別
聲明一個變量就是在內存空間划出一塊合適的空間
聲明一個數組就是在內存空間划出一串連續的空間
數組的基本要素
標識符:數組的名稱,用於區分不同的數組
數組元素:向數組中存放的數據
元素下標:對數組元素進行編號,從0開始,數組中的每個元素都可以通過下標來訪問
元素類型:數組元素的數據類型
2 如何使用數組
2.1 使用數組的步驟
步驟一:聲明數組
數據類型[] 數組名;
或者
數據類型 數組名[]
步驟二:分配空間
數組名 = new 數組類型[數組長度]
步驟一和步驟二可以合並
數據類型[] 數組名= new 數據類型[數組長度];
步驟三 賦值
數組名[下標值]
或
數據類型[] 數組名 ={值1,值2..};
或
數據類型[] 數組名 = new 數組類型[]值1,值2..};
步驟四 使用
public class StudenAvg {
public static void main(String[] args) {
int score[] = new int[30];
//從外部獲得學生成績
Scanner input = new Scanner(System.in);
//初始化
for (int i = 0; i < score.length; i++) {
System.out.println("請輸入第"+(i+1)+"學生的成績:");
score[i] = input.nextInt();
}
//求平均值
double avg = 0;
for (int i = 0; i < score.length; i++) {
avg+=score[i];
}
avg=avg/30.0;
System.out.println("平均值是:"+avg);
}
}
注:
數組長度:數組名.length
2.2 常見錯誤
錯誤一:數組下標從0開始
錯誤二:沒有指明數組大小
public class ErrorDemo1 {
public static void main(String[ ] args){
int[ ] score = new int[ ];
score[0] = 89;
score[1] = 63;
System.out.println(score[0]);
}
}
錯誤三:數組越界
public class ErrorDemo2 {
public static void main(String[ ] args) {
int[ ] scores = new int[2];
scores[0] = 90;
scores[1] = 85;
scores[2] = 65;
System.out.println(scores[2]);
}
}
錯誤四:賦值出錯
public static void main(String[ ] args){
int[ ] score = new int[5];
score = {60, 80, 90, 70, 85};
int[ ] score2;
score2 = {60, 80, 90, 70, 85};
}
2.3 編程訓練
有一個數列:8,4,2,1,23,344,12
循環輸出數列的值
求數列中所有數值的和
猜數游戲:從鍵盤中任意輸入一個數據,判斷數列中是否包含此數
實現
/**
*
* @author wangshaohua
* 功能:有一個數列:8,4,2,1,23,344,12
循環輸出數列的值
求數列中所有數值的和
猜數游戲:從鍵盤中任意輸入一個數據,判斷數列中是否包含此數
*
*/
public class Show1 {
public static void main(String[] args) {
int[] arr = {8,4,2,1,23,344,12};
//循環輸出數列的值
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"\t");
}
//求數列中所有數值的和
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum+=i;
}
System.out.println("\n數列中所有數值的和:"+sum);
//從鍵盤中任意輸入一個數據,判斷數列中是否包含此數
Scanner input = new Scanner(System.in);
System.out.println("請輸入需要判斷的數值:");
int num = input.nextInt();
int index = -1;//相等值的下標
for (int i = 0; i < arr.length; i++) {
if(num==arr[i]){
index = i;
break;
}else {
continue;
}
}
if(index==-1){
System.out.println("你輸入的值不在數列當中");
}else {
System.out.println("你輸入的值在數列當中,序號是:"+index);
}
}
}
3 數組的應用
3.1 求最大值
問題:從鍵盤輸入本次Java考試五位學生的成績,求考試成績最高分
實現
ublic class MaxScore {
/**
* 求數組最大值
*/
public static void main(String[] args) {
int[] scores = new int[5];
int max = 0; //記錄最大值
System.out.println("請輸入5位學員的成績:");
Scanner input = new Scanner(System.in);
for(int i = 0; i < scores.length; i++){
scores[i] = input.nextInt();
}
//計算最大值
max = scores[0];
for(int i = 1; i < scores.length; i++){
if(scores[i] > max){
max = scores[i];
}
}
System.out.println("考試成績最高分為:" + max);
}
}
3.2 數組的排序
問題:循環錄入5位學員成績,進行升序排列后輸出結果
分析:
使用java.util.Arrays類
java.util包提供了許多工具類
Arrays類提供操作數組的方法,例排序、查詢
Arrays類的sort()方法: 對數組進行升序排列
實現
public class ScoreSort {
public static void main(String[] args) {
int[] scores = new int[5]; //成績數組
Scanner input = new Scanner(System.in);
System.out.println("請輸入5位學員的成績:");
//循環錄入學員成績
for(int i = 0; i < scores.length; i++){
scores[i] = input.nextInt();
}
Arrays.sort(scores); //對數組進行升序排序
System.out.print("學員成績按升序排列:");
//利用循環輸出學員成績
for(int i = 0; i < scores.length; i++){
System.out.print(scores[i] + " ");
}
}
}
3.3 向數組插入元素
問題:有一組學員的成績{99,85,82,63,60},將它們按升序排列。要增加一個學員的成績,將它插入成績序列,並保持升序。
分析:
將成績序列保存在長度為6的數組中
通過比較找到插入位置
將該位置后的元素后移一個位置
將增加的學員成績插入到該位置
實現
public class Insert {
public static void main(String[] args) {
int[] list = new int[6]; // 長度為為6的數組
list[0] = 99;
list[1] = 85;
list[2] = 82;
list[3] = 63;
list[4] = 60;
int index = list.length; //保存新增成績插入位置
System.out.println("請輸入新增成績: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt(); // 輸入要插入的數據
//找到新元素的插入位置
for(int i = 0; i < list.length; i++){
if(num > list[i]){
index = i;
break;
}
}
//元素后移
for(int j = list.length-1; j > index; j--){
list[j] = list[j-1]; //index下標開始的元素后移一個位置
}
list[index] = num;//插入數據
System.out.println("插入成績的下標是:"+index);
System.out.println("插入后的成績信息是: ");
for (int k = 0; k < list.length; k++) { // 循環輸出目前數組中的數據
System.out.print(list[k] + "\t");
}
}
}
4 總結
數組有哪些特點?
使用數組的四個步驟?
如何實現數組的排序?
如何求數組最大/最小值?
如何向數組中插入一個元素?