數(shù)組的基本概念和基本用法

一.數(shù)組基本概念

1.1 什么是數(shù)組

數(shù)組是用來存儲多個連續(xù)數(shù)據(jù)類型相同的數(shù)據(jù)

1.2 什么是數(shù)組元素

數(shù)組中的每個數(shù)據(jù)就是數(shù)組元素

1.3 什么是數(shù)組長度

數(shù)組的元素個數(shù)就是數(shù)組長度

數(shù)組的長度在為數(shù)組元素分配空間時就已經(jīng)確定了大小

二.使用數(shù)組

2.1 使用數(shù)組分4步:

        1. 定義數(shù)組

        2. 為數(shù)組元素分配內(nèi)存

        3. 數(shù)組元素初始化

        4.使用數(shù)組

例如:班級5個學(xué)生java成績,求成績平均分

import java.util.Scanner;

public class s {
    public static void main(String[] args) {
        //第一步:定義數(shù)組,數(shù)組的名稱是score
        int []score;//或者   int score[]
        //第二步:為數(shù)組元素分空間
        score = new int[5];
        //第三步:元素初始化
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i <score.length ; i++) {
            System.out.println("請輸入第"+(i+1)+"位同學(xué)的成績");
            score[i]=scanner.nextInt();
        }
        //第四步:使用數(shù)組求成績
        int sum =0;
        int avg = 0;
        for (int i = 0; i <score.length ; i++) {
            sum +=score[i];
        }
        avg = sum/score.length;
        System.out.println("5位同學(xué)的平均成績是:"+avg);
    }
}

!引用老師所畫的內(nèi)存分配圖
1657600125127.png

三.數(shù)組的數(shù)據(jù)結(jié)構(gòu)(線性表)

  • 線性表,全名為線性存儲結(jié)構(gòu)。使用線性表存儲數(shù)據(jù)的方式可以這樣理解,即“把所有數(shù)據(jù)用一根線兒串起來,再存儲到物理空間中”。

四.數(shù)組的基本用法

4.1 求一組數(shù)據(jù)中的最大值,最小值

public class Example06 {
    public static void main(String[] args) {
        //定義一個數(shù)組并分配內(nèi)存與初始化
        int score[] = new int[]{67, 78, 65, 88, 79};
        // 求最低分,最高分
        int min = score[0];
        int max=score[0];
        for (int i = 0; i < score.length; i++) {
            //循環(huán)判斷
            if (score[i] < min) {
                min = score[i];
            }
            //循環(huán)判斷
            if (score[i]>max){
                max = score[i];
            }
        }
        //輸出結(jié)果
        System.out.println(min);
        System.out.println(max);
    }
}

4.2 求一組數(shù)據(jù)的和與平均值

public class Example07 {
    public static void main(String[] args) {
        //定義數(shù)組,并初始化數(shù)組和分配內(nèi)存空間
        int score[] = new int[]{67,78,65,88,79};
        int sum = 0;
        for (int i = 0; i <score.length ; i++) {
            //每循環(huán)一次便累加上一次結(jié)果,循環(huán)結(jié)束,求和結(jié)束
            sum+=score[i];
        }
        //輸出數(shù)組中所有元素的和
        System.out.println(sum);
        //輸出數(shù)組的平均值
        System.out.println(sum/score.length);
    }
}

4.3 遍歷數(shù)組,輸出數(shù)組

4.3.1 普通遍歷數(shù)組的方法
public class Example09 {
    public static void main(String[] args) {
        int score[] = new int[]{67,78,65,88,79};
        for (int i = 0; i <score.length ; i++) {
            System.out.println(score[i]);
        }

    }
}
4.3.2 增強for循環(huán)遍歷數(shù)組
public class Example10 {
    //增強for循環(huán)遍歷數(shù)組
    public static void main(String[] args) {
        int score[] = new int[]{67, 78, 65, 88, 79};
        for(int s :score){
            System.out.println(s);
        }
    }
}

4.4排序

4.4.1冒泡排序
  • 每一趟只能確定將一個數(shù)歸位。即第一趟只能確定將末位上的數(shù)歸位,第二趟只能將倒數(shù)第 2 位上的數(shù)歸

位,依次類推下去。如果有 n 個數(shù)進行排序,只需將 n-1 個數(shù)歸位,也就是要進行 n-1 趟操作。

  • 而 “每一趟 ” 都需要從第一位開始進行相鄰的兩個數(shù)的比較,將較大的數(shù)放后面,比較完畢之后向后挪一位

繼續(xù)比較下面兩個相鄰的兩個數(shù)大小關(guān)系,重復(fù)此步驟,直到最后一個還沒歸位的數(shù)。


s冒泡排序.gif
import java.util.Scanner;

public class Example11 {
    public static void main(String[] args) {
        //冒泡排序法
        Scanner s = new Scanner(System.in);
        int score[] = new int[5];
        for (int i = 0; i < 5; i++) {
            System.out.println("請輸入需要排序的" + (i + 1) + "個人的成績;");
            score[i] = s.nextInt();
        }
        long start =System.currentTimeMillis();
        for (int i = 0; i < score.length - 1; i++) {
            for (int j = 0; j < score.length - 1 - i; j++) {
                if (score[j] > score[j + 1]) {
                    int m = score[j];
                    score[j] = score[j + 1];
                    score[j + 1] = m;
                }
            }
        }
    }
}
4.4.2 選擇排序

(1)每次排序的時候都需要尋找第n小的數(shù)據(jù),并且和array[n-1]發(fā)生交換

(2)等到n個數(shù)據(jù)都排序好,那么選擇排序結(jié)束。


s選擇排序.gif
import java.util.Scanner;

public class Example12 {
    public static void main(String[] args) {
        //選擇排序
        Scanner s = new Scanner(System.in);
        int score[] = new int[5];
        for (int i = 0; i < score.length; i++) {
            System.out.println("請輸入第" + (i + 1) + "的成績");
            score[i] = s.nextInt();
        }
        for (int i = 0; i < score.length - 1; i++) {//每次循環(huán)都會找出最小的數(shù)
            int minindex = i;//記錄最小數(shù)的下標(biāo)
            int min = score[i];//記錄最小數(shù)
            for (int j =i+1; j < score.length; j++) {//每次循環(huán)都會找出最小的數(shù)
                if (score[j]<min){//如果當(dāng)前數(shù)比最小數(shù)小,則更新最小數(shù)
                  //記錄最小值小標(biāo)
                    minindex=j;;//更新最小數(shù)的下標(biāo)
                    min =score[j];//更新最小數(shù)
                }
            }
            int t = score[i];
            score[i]=score[minindex];//將最小數(shù)放到最前面
            score[minindex]= t;
        }
        for (int i = 0; i < score.length; i++) {
            System.out.println(score[i]);
        }
    }
}
4.4.3 插入排序

(1)首先對數(shù)組的前兩個數(shù)據(jù)進行從小到大的排序。
(2)接著將第3個數(shù)據(jù)與排好序的兩個數(shù)據(jù)比較,將第3個數(shù)據(jù)插入到合適的位置。
(3)然后,將第4個數(shù)據(jù)插入到已排好序的前3個數(shù)據(jù)中。
(4)不斷重復(fù)上述過程,直到把最后一個數(shù)據(jù)插入合適的位置。最后,便完成了對原始數(shù)組從小到大的排序。


s插入排序.gif
import java.util.Scanner;

public class Example13 {
    public static void main(String[] args) {
        //少量數(shù)據(jù)插入排序
        Scanner  s = new Scanner(System.in);
        int score[]=new int[5];
        for (int i = 0; i <score.length ; i++) {
            System.out.println("請輸入第"+(i+1)+"同學(xué)的成績");
            score[i]=s.nextInt();
        }
        for (int i = 1; i <score.length ; i++) {
            int j=i;
           while (j>0){
               if (score[j]<score[j-1]){
                   int t = score[j];
                   score[j]=score[j-1];
                   score[j-1]=t;
                   j--;
               }else{
                   break;
               }
           }
        }
        for (int i = 0; i <score.length; i++) {
            System.out.println(score[i]);
        }
  }
}

4.5 二分查找

import java.util.Scanner;

public class Example14 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int score[] = new int[5];
        for (int i = 0; i < score.length; i++) {
            System.out.println("請輸入第" + (i + 1) + "學(xué)生的成績:");
            score[i] = scanner.nextInt();
        }
        //插入排序
        for (int i = 1; i < score.length; i++) {
            int j = i;
            while (j > 0) {
                if (score[j] < score[j - 1]) {
                    int t = score[j];
                    score[j] = score[j - 1];
                    score[j - 1] = t;
                    j--;
                } else {
                    break;
                }
            }
        }
        System.out.println("請輸入你想要查的成績獲得排名:");
        int n = scanner.nextInt();
        int index = binarySearch(score, n);
        if (index == -1) {
            System.out.println("沒有找到相應(yīng)的成績排名:" + index);
        } else {
            System.out.println("找到了相應(yīng)成績的排名:" + (index + 1));
        }
        //遍歷輸出所有人成績
        for (int i = 0; i < score.length; i++) {
            System.out.print(score[i]);
            System.out.print(" ");
        }
    }
    // 二分查找
    public static int binarySearch(int[] scrArray, int n ) {
        int first = 0;
        int last = scrArray.length - 1;
        while (first <= last) {
            int middle = (first + last) >>> 1;
            if (n == scrArray[middle]) {
                return middle;
            } else if (n > scrArray[middle]) {
                first = middle + 1;
            } else {
                last = middle - 1;
            }
        }
        return -1;
    }
}

4.6 系統(tǒng)提供的Arrays類

4.6.1 比較數(shù)組是否相同

public static void main(String[] args) {
        //Arrays 應(yīng)用類   比較數(shù)組是否相同
        int arr1[] = new int[]{56,12,15,46,84,75,56};
        int arr2[] = new int[]{56,12,15,46,84,75,56};
        int arr3[] = new int[]{56,12,15,46,84};
        System.out.println(Arrays.equals(arr1,arr2));
        System.out.println(Arrays.equals(arr1,arr3));
    }

4.6.2數(shù)組排序

public static void main(String[] args) {
        //Arrays 應(yīng)用類   數(shù)組內(nèi)元素升序排列 并以數(shù)組形式輸出
        int arr1[] = new int[]{56,12,15,46,84,75,56};
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));
    }

4.6.3 充滿數(shù)組

//Arrays 應(yīng)用類   充滿數(shù)組
public static void main(String[] args) {
        int arr1[] = new int[9];
        Arrays.fill(arr1,20);
        System.out.println(Arrays.toString(arr1));
    }

4.6.4 生成新數(shù)組,并設(shè)置新長度

public static void main(String[] args) {
        // Arrays 應(yīng)用類  復(fù)制并生成新的長度的數(shù)組
        int arr1[] = new int[]{56,12,15,46,84,75};
        int arr2[] = Arrays.copyOf(arr1,15);
        System.out.println(Arrays.toString(arr2));
    }

4.6.5 在數(shù)組中查找并返回下標(biāo)

public static void main(String[] args) {
        //Arrays 應(yīng)用類 在數(shù)組中查找 并返回下標(biāo)
        int arr1[] = new int[]{56,12,15,46,84,75,56};
        Arrays.sort(arr1);
        System.out.println(Arrays.binarySearch(arr1,12));
        System.out.println(Arrays.binarySearch(arr1,15));
        System.out.println(Arrays.binarySearch(arr1,50));
        System.out.println(Arrays.binarySearch(arr1,60));
    }

4.6.6以數(shù)組形式輸出數(shù)組

import java.util.Arrays;

public class Example09 {
    public static void main(String[] args) {
        int score[] = new int[]{67,78,65,88,79};
        System.out.println(Arrays.toString(score));
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容