第八章 數組


一、本章目標

  • 掌握數組的基本用法

  • 掌握數組的幾種典型應用

二、內容

1 數組概述

1.1 為什么需要數組

  • 問題:Java考試結束后,老師給張浩分配了一項任務,讓他計算全班(30人)的平均分

  • 解決方案    

        

  • 缺點

    • 變量太多

    • 不利於數據處理

1.2 Java中的數組

什么是數組

    數組是一個變量,存儲相同數據類型的一組數據

數組與變量的區別

  • 聲明一個變量就是在內存空間划出一塊合適的空間

  • 聲明一個數組就是在內存空間划出一串連續的空間

數組的基本要素

  • 標識符:數組的名稱,用於區分不同的數組

  • 數組元素:向數組中存放的數據

  • 元素下標:對數組元素進行編號,從0開始,數組中的每個元素都可以通過下標來訪問

  • 元素類型:數組元素的數據類型  

2 如何使用數組

2.1 使用數組的步驟

步驟一:聲明數組

 
 
 
         
  1. 數據類型[] 數組名;

  2. 或者

  3. 數據類型 數組名[]

步驟二:分配空間

 
 
 
         
  1. 數組名 = new 數組類型[數組長度]

步驟一和步驟二可以合並

 
 
 
         
  1. 數據類型[] 數組名= new 數據類型[數組長度];

步驟三 賦值

 
 
 
         
  1. 數組名[下標值]

  2. 數據類型[] 數組名 ={值1,值2..};

  3. 數據類型[] 數組名 = new 數組類型[]值1,值2..};

步驟四 使用

 
 
 
         
  1. public class StudenAvg {

  2. public static void main(String[] args) {

  3. int score[] = new int[30];

  4. //從外部獲得學生成績

  5. Scanner input = new Scanner(System.in);

  6. //初始化

  7. for (int i = 0; i < score.length; i++) {

  8. System.out.println("請輸入第"+(i+1)+"學生的成績:");

  9. score[i] = input.nextInt();

  10. }

  11. //求平均值

  12. double avg = 0;

  13. for (int i = 0; i < score.length; i++) {

  14. avg+=score[i];

  15. }

  16. avg=avg/30.0;

  17. System.out.println("平均值是:"+avg);

  18. }

  19. }

注:

    數組長度:數組名.length

2.2 常見錯誤

錯誤一:數組下標從0開始

錯誤二:沒有指明數組大小

 
 
 
         
  1. public class ErrorDemo1 {

  2.     public static void main(String[ ] args){

  3.          int[ ] score = new int[ ];

  4.          score[0] = 89;

  5.          score[1] = 63;

  6.          System.out.println(score[0]);

  7.    }

  8. }

錯誤三:數組越界

 
 
 
         
  1. public class ErrorDemo2 {

  2. public static void main(String[ ] args) {

  3. int[ ] scores = new int[2];

  4. scores[0] = 90;

  5. scores[1] = 85;

  6. scores[2] = 65;

  7. System.out.println(scores[2]);

  8. }

  9. }

錯誤四:賦值出錯

 
 
 
         
  1. public static void main(String[ ] args){

  2.       int[ ] score = new int[5];

  3.       score = {60, 80, 90, 70, 85};

  4.      

  5.       int[ ] score2;

  6.       score2 = {60, 80, 90, 70, 85};

  7. }

2.3 編程訓練

有一個數列:8,4,2,1,23,344,12

  • 循環輸出數列的值

  • 求數列中所有數值的和

  • 猜數游戲:從鍵盤中任意輸入一個數據,判斷數列中是否包含此數

實現

 
 
 
         
  1. /**

  2. *

  3. * @author wangshaohua

  4. * 功能:有一個數列:8,4,2,1,23,344,12

  5. 循環輸出數列的值

  6. 求數列中所有數值的和

  7. 猜數游戲:從鍵盤中任意輸入一個數據,判斷數列中是否包含此數

  8. *

  9. */

  10. public class Show1 {

  11. public static void main(String[] args) {

  12. int[] arr = {8,4,2,1,23,344,12};

  13. //循環輸出數列的值

  14. for (int i = 0; i < arr.length; i++) {

  15. System.out.print(arr[i]+"\t");

  16. }

  17. //求數列中所有數值的和

  18. int sum = 0;

  19. for (int i = 0; i < arr.length; i++) {

  20. sum+=i;

  21. }

  22. System.out.println("\n數列中所有數值的和:"+sum);

  23. //從鍵盤中任意輸入一個數據,判斷數列中是否包含此數

  24. Scanner input = new Scanner(System.in);

  25. System.out.println("請輸入需要判斷的數值:");

  26. int num = input.nextInt();

  27. int index = -1;//相等值的下標

  28. for (int i = 0; i < arr.length; i++) {

  29. if(num==arr[i]){

  30. index = i;

  31. break;

  32. }else {

  33. continue;

  34. }

  35. }

  36. if(index==-1){

  37. System.out.println("你輸入的值不在數列當中");

  38. }else {

  39. System.out.println("你輸入的值在數列當中,序號是:"+index);

  40. }

  41. }

  42. }

3 數組的應用

3.1 求最大值

問題:從鍵盤輸入本次Java考試五位學生的成績,求考試成績最高分

實現

 
 
 
         
  1. ublic class MaxScore {

  2. /**

  3. * 求數組最大值

  4. */

  5. public static void main(String[] args) {

  6. int[] scores = new int[5];

  7. int max = 0; //記錄最大值

  8. System.out.println("請輸入5位學員的成績:");

  9. Scanner input = new Scanner(System.in);

  10. for(int i = 0; i < scores.length; i++){

  11. scores[i] = input.nextInt();

  12. }

  13. //計算最大值

  14. max = scores[0];

  15. for(int i = 1; i < scores.length; i++){

  16. if(scores[i] > max){

  17. max = scores[i];

  18. }

  19. }

  20. System.out.println("考試成績最高分為:" + max);

  21. }

  22. }

3.2 數組的排序

問題:循環錄入5位學員成績,進行升序排列后輸出結果

分析:

  • 使用java.util.Arrays類

    • java.util包提供了許多工具類

    • Arrays類提供操作數組的方法,例排序、查詢

    • Arrays類的sort()方法: 對數組進行升序排列

實現

 
 
 
         
  1. public class ScoreSort {

  2. public static void main(String[] args) {

  3. int[] scores = new int[5]; //成績數組

  4. Scanner input = new Scanner(System.in);

  5. System.out.println("請輸入5位學員的成績:");

  6. //循環錄入學員成績

  7. for(int i = 0; i < scores.length; i++){

  8. scores[i] = input.nextInt();

  9. }

  10. Arrays.sort(scores); //對數組進行升序排序

  11. System.out.print("學員成績按升序排列:");

  12. //利用循環輸出學員成績

  13. for(int i = 0; i < scores.length; i++){

  14. System.out.print(scores[i] + " ");

  15. }

  16. }

  17. }

3.3 向數組插入元素


問題:有一組學員的成績{99,85,82,63,60},將它們按升序排列。要增加一個學員的成績,將它插入成績序列,並保持升序。


分析:

  • 將成績序列保存在長度為6的數組中

  • 通過比較找到插入位置

  • 將該位置后的元素后移一個位置

  • 將增加的學員成績插入到該位置 

實現

 
 
 
         
  1. public class Insert {

  2. public static void main(String[] args) {

  3. int[] list = new int[6]; // 長度為為6的數組

  4. list[0] = 99;

  5. list[1] = 85;

  6. list[2] = 82;

  7. list[3] = 63;

  8. list[4] = 60;

  9. int index = list.length; //保存新增成績插入位置

  10. System.out.println("請輸入新增成績: ");

  11. Scanner input = new Scanner(System.in);

  12. int num = input.nextInt(); // 輸入要插入的數據

  13. //找到新元素的插入位置

  14.        for(int i = 0; i < list.length; i++){

  15.            if(num > list[i]){

  16.                index = i;

  17.                break;

  18.            }

  19.        }

  20. //元素后移

  21.        for(int j = list.length-1; j > index; j--){

  22.            list[j] = list[j-1]; //index下標開始的元素后移一個位置

  23.        }

  24. list[index] = num;//插入數據

  25. System.out.println("插入成績的下標是:"+index);

  26. System.out.println("插入后的成績信息是: ");

  27. for (int k = 0; k < list.length; k++) { // 循環輸出目前數組中的數據

  28. System.out.print(list[k] + "\t");

  29. }

  30. }

  31. }

4 總結

  • 數組有哪些特點?

  • 使用數組的四個步驟?

  • 如何實現數組的排序?

  • 如何求數組最大/最小值?

  • 如何向數組中插入一個元素?















免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM