数组的根本概念和根本用法

源代码 2024-9-18 14:10:31 29 0 来自 中国
一.数组根本概念

1.1 什么是数组

数组是用来存储多个一连数据范例雷同的数据
1.2 什么是数组元素

数组中的每个数据就是数组元素
1.3 什么是数组长度

数组的元素个数就是数组长度
数组的长度在为数组元素分配空间时就已经确定了巨细
二.利用数组

2.1 利用数组分4步:

        1. 界说数组        2. 为数组元素分配内存        3. 数组元素初始化        4.利用数组比方:班级5个弟子java成绩,求成绩平均分
import java.util.Scanner;public class s {    public static void main(String[] args) {        //第一步:界说数组,数组的名称是score        int []score;//大概   int score[]        //第二步:为数组元素分空间        score = new int[5];        //第三步:元素初始化        Scanner scanner = new Scanner(System.in);        for (int i = 0; i <score.length ; i++) {            System.out.println("请输入第"+(i+1)+"位同学的成绩");            score=scanner.nextInt();        }        //第四步:利用数组求成绩        int sum =0;        int avg = 0;        for (int i = 0; i <score.length ; i++) {            sum +=score;        }        avg = sum/score.length;        System.out.println("5位同学的平均成绩是:"+avg);    }}

!引用老师所画的内存分配图三.数组的数据结构(线性表)


  • 线性表,全名为线性存储结构。利用线性表存储数据的方式可以如许明确,即“把所有数据用一根线儿串起来,再存储到物理空间中”。
四.数组的根本用法

4.1 求一组数据中的最大值,最小值

public class Example06 {    public static void main(String[] args) {        //界说一个数组并分配内存与初始化        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++) {            //循环判定            if (score < min) {                min = score;            }            //循环判定            if (score>max){                max = score;            }        }        //输出效果        System.out.println(min);        System.out.println(max);    }}4.2 求一组数据的和与平均值

public class Example07 {    public static void main(String[] args) {        //界说数组,并初始化数组和分配内存空间        int score[] = new int[]{67,78,65,88,79};        int sum = 0;        for (int i = 0; i <score.length ; i++) {            //每循环一次便累加上一次效果,循环竣事,求和竣事            sum+=score;        }        //输出数组中所有元素的和        System.out.println(sum);        //输出数组的平均值        System.out.println(sum/score.length);    }}4.3 遍历数组,输出数组

4.3.1 平凡遍历数组的方法

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);        }    }}4.3.2 加强for循环遍历数组

public class Example10 {    //加强for循环遍历数组    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冒泡排序


  • 每一趟只能确定将一个数归位。即第一趟只能确定将末位上的数归位,第二趟只能将倒数第 2 位上的数归
位,依次类推下去。假如有 n 个数举行排序,只需将 n-1 个数归位,也就是要举行 n-1 趟利用。

  • 而 “每一趟 ” 都须要从第一位开始举行相邻的两个数的比力,将较大的数放背面,比力完毕之后向后挪一位
继承比力下面两个相邻的两个数巨细关系,重复此步调,直到末了一个还没归位的数。

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 = 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小的数据,而且和array[n-1]发生互换
(2)比及n个数据都排序好,那么选择排序竣事。

3.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 = s.nextInt();        }        for (int i = 0; i < score.length - 1; i++) {//每次循环都会找出最小的数            int minindex = i;//纪录最小数的下标            int min = score;//纪录最小数            for (int j =i+1; j < score.length; j++) {//每次循环都会找出最小的数                if (score[j]<min){//假如当前数比最小数小,则更新最小数                  //纪录最小值小标                    minindex=j;;//更新最小数的下标                    min =score[j];//更新最小数                }            }            int t = score;            score=score[minindex];//将最小数放到最前面            score[minindex]= t;        }        for (int i = 0; i < score.length; i++) {            System.out.println(score);        }    }}4.4.3 插入排序

(1)起首对数组的前两个数据举行从小到大的排序。
(2)接着将第3个数据与排好序的两个数据比力,将第3个数据插入到符合的位置。
(3)然后,将第4个数据插入到已排好序的前3个数据中。
(4)不停重复上述过程,直到把末了一个数据插入符合的位置。末了,便完成了对原始数组从小到大的排序。

import java.util.Scanner;public class Example13 {    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=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);        }  }}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) + "弟子的成绩:");            score = 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("没有找到相应的成绩排名:" + index);        } else {            System.out.println("找到了相应成绩的排名:" + (index + 1));        }        //遍历输出所有人成绩        for (int i = 0; i < score.length; i++) {            System.out.print(score);            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 体系提供的Arrays类

4.6.1 比力数组是否雷同

public static void main(String[] args) {        //Arrays 应用类   比力数组是否雷同        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数组排序

public static void main(String[] args) {        //Arrays 应用类   数组内元素升序分列 并以数组形式输出        int arr1[] = new int[]{56,12,15,46,84,75,56};        Arrays.sort(arr1);        System.out.println(Arrays.toString(arr1));    }4.6.3 布满数组

//Arrays 应用类   布满数组public static void main(String[] args) {        int arr1[] = new int[9];        Arrays.fill(arr1,20);        System.out.println(Arrays.toString(arr1));    }4.6.4 天生新数组,并设置新长度

public static void main(String[] args) {        // Arrays 应用类  复制并天生新的长度的数组        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 在数组中查找并返回下标

public static void main(String[] args) {        //Arrays 应用类 在数组中查找 并返回下标        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以数组形式输出数组

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));    }}
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-19 00:22, Processed in 0.202148 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表