• Java数组


    1.数组概述

            数组是具有相同数据类型的一组数据的集合。虽然基本数据类型不是对象,但由基本数据类型组成的数组却是对象,所以Java中将数组看做一个对象。

            Java中数组分为一维数组,二维数组。。。。

    2.一维数组的创建及使用

    2.1 创建一维数组

            数组作为对象允许使用关键字【new】进行内存分配。在使用数组前必须先定义数组变量所属的类型。

    2.1.1 声明数组

    2.1.1.1 数组元素类型 数组名字[];
    1. int arr[]; //声明int型数组,数组中的每个元素都必须是int类型
    2. String str[]; //声明String型数组,每个元素都必须是String类型
    2.1.1.2 数组元素类型[] 数组名字;
    1. int[] arr;
    2. String[] str;

    2.1.2 为数组分配内存空间 

            数组名字 = new 数组元素的类型[数组元素的个数]
    arr = new int[5];

            使用new关键字为数组分配内存时,整型数组中各元素初始值都是0.,字符型数组中各元素初始值为‘’,String型数组的初始值为null,float型数组初始值为0.0;

    2.1.3 声明的同时为数组分配内存

    2.1.3.1 数组元素的类型 数组名= new 数组元素的类型[数组元素的个数]
    2.1.3.2 String str= new String[5]

    2.2 初始化一维数组

    2.2.1 int arr[]=new int[]{1,2,3,4,5};

    2.2.2 int arr[]= {1,2,3,4,5};

    2.3 使用一维数组

    1. public class GetDay {
    2. public static void main(String[] args) {
    3. int days[] = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    4. for (int i = 0; i < 12; i++) {
    5. System.out.println((i + 1) + "月有" + days[i] + "天");
    6. }
    7. }
    8. }
    9. 输出:
    10. 1月有31
    11. 2月有28
    12. 3月有31
    13. 4月有30
    14. 5月有31
    15. 6月有30
    16. 7月有31
    17. 8月有31
    18. 9月有30
    19. 10月有31
    20. 11月有30
    21. 12月有31

    3.二维数组的创建及使用

            如果一维数组中的各个元素仍然是一个数组,那么它就是一个二维数组。

    3.1 二维数组的创建

    3.1.1 数组元素的类型 数组名[][];

    3.1.2 数组元素的类型[][] 数组名;

    3.2 二维数组的初始化

            type arrayname[][] = {value1,value2,value3.....}

    int myarr[][]={{1,2,3},{4,5}}

    3.3 使用二维数组 

    1. public class Matrix {
    2. public static void main(String[] args) {
    3. int arr[][] = new int[3][4]; //定义二维数组
    4. //遍历二维数组
    5. for (int i = 0; i < arr.length; i++) {
    6. for (int j = 0; j < arr[i].length; j++) {
    7. System.out.print(arr[i][j] + "\t");
    8. }
    9. System.out.println();
    10. }
    11. }
    12. }
    13. 输出:
    14. 0 0 0 0
    15. 0 0 0 0
    16. 0 0 0 0

    4.数组的基本操作

            java.util包的Arrays类包含了操作数组的各种方法。

    4.1 遍历数组

    for循环语句:

    1. public class Matrix {
    2. public static void main(String[] args) {
    3. int arr[][] = new int[][]{{1},{2,3},{4,5,6}}; //定义二维数组
    4. //遍历二维数组
    5. for (int i = 0; i < arr.length; i++) {
    6. for (int j = 0; j < arr[i].length; j++) {
    7. System.out.print(arr[i][j]);
    8. }
    9. System.out.println();
    10. }
    11. }
    12. }
    13. 输出:
    14. 1
    15. 23
    16. 456

    foreach循环语句:

    1. public class Tautog {
    2. public static void main(String[] args) {
    3. int arrs[][] = {{4, 3}, {2, 1, 0}};
    4. System.out.println("数组中的元素是:");
    5. for (int arr[] : arrs) {
    6. for (int ar : arr) {
    7. System.out.print(ar);
    8. }
    9. }
    10. }
    11. }
    12. 输出:
    13. 数组中的元素是:
    14. 43210

    4.2 填充替换数组元素

            通过Arrays类的静态方法fill()来对数组中的元素进行替换,并不会生成新的数组。

            4.2.1 Arrays.fill(int[] arr, int vaule)

    1. import java.util.Arrays;
    2. public class Swap {
    3. public static void main(String[] args) {
    4. int arrs[] = new int[5];
    5. Arrays.fill(arrs, 8);
    6. for (int arr : arrs) {
    7. System.out.print(arr + "\t");
    8. }
    9. }
    10. }
    11. 输出:
    12. 8 8 8 8 8

            4.2.2 Arrays.fill(int[] arr, int fromIndex, int toIndex, int value) 

    1. import java.util.Arrays;
    2. public class Displace {
    3. public static void main(String[] args) {
    4. int arrs[] = new int[]{45, 12, 2, 10};
    5. Arrays.fill(arrs, 0, 2, 9);
    6. for (int arr : arrs) {
    7. System.out.print(arr + "\t");
    8. }
    9. }
    10. }
    11. 输出:
    12. 9 9 2 10

    4.3 对数组进行排序

            通过Arrays类的静态方法sort()可以实现对数组进行排序;并不会生成新的数组。

            Arrays.sort(arr)

    1. import java.util.Arrays;
    2. public class Taxis {
    3. public static void main(String[] args) {
    4. int arrs[] = new int[]{23, 42, 12, 8, 5};
    5. Arrays.sort(arrs);
    6. for (int i = 0; i < arrs.length; i++) {
    7. System.out.print(arrs[i]+"\t");
    8. }
    9. }
    10. }
    11. 输出:
    12. 5 8 12 23 42

    4.4 复制数组

            Arrays类的copyOf()方法和copyOfRange()方法可以实现对数组的复制。会生成新数组。

            4.4.1 Arrays.copyOf(arr,int newlength)

                    复制数组至指定长度;arr:要进行复制的数组, newlength:复制后新数组的长度,若新数组长度大于原数组,则用0填充(char型用null填充),会生成新数组。

                    

    1. import java.util.Arrays;
    2. public class Cope {
    3. public static void main(String[] args) {
    4. int arrs[] = {23, 43, 12};
    5. int arrs2[] = Arrays.copyOf(arrs, 5); //复制会生成新数组
    6. for (int i = 0; i < arrs2.length; i++) {
    7. System.out.println("第" + (i + 1) + "个数是:" + arrs2[i]);
    8. }
    9. }
    10. }
    11. 输出:
    12. 1个数是:23
    13. 2个数是:43
    14. 3个数是:12
    15. 4个数是:0
    16. 5个数是:0

            4.4.2 Arrays.copyOfRange(arr, int fromIndex, int toIndex)

                    将指定数组的指定长度复制到一个新数组中。arr:要进行复制的数组对象; fromIndex:指定开始复制的索引位置;toIndex:指定结束复制的索引位置。

    1. import java.util.Arrays;
    2. public class Repeat {
    3. public static void main(String[] args) {
    4. int arrs[] = new int[]{23, 42, 12, 84, 10};
    5. int newArrs[] = Arrays.copyOfRange(arrs, 0, 3);
    6. for (int i = 0; i < newArrs.length; i++) {
    7. if (i == 2) {
    8. System.out.print(newArrs[i]);
    9. } else {
    10. System.out.print(newArrs[i] + ",");
    11. }
    12. }
    13. }
    14. }
    15. 输出:
    16. 23,42,12

    4.5 数组查询

            Arrays类的binarySearch()方法,可以使用二分搜索法来搜索指定数组,以获得指定对象。该方法返回要搜索元素的索引值。

    4.5.1 Arrays.binarySearch(object[] arr, object key)

            arr:要所搜的数组,key:要搜索的值。如果key包含在数组中,则返回索引值,否则返回-1或“-”。

    1. int arrs[] = new int[]{4, 25, 10};
    2. Arrays.sort(arrs);
    3. int index = Arrays.binarySearch(arrs, 25);
    4. System.out.println("25的索引值是:" + index);
    5. 输出:
    6. 25的索引值是:2

    4.5.2 Arrays.binarySearch(object[] arrs, int fromIndex, int toIndex, object key) 

            在指定的索引范围内搜索元素。

    1. String str[]= new String[]{"ab","cd","ef","yz"};
    2. Arrays.sort(str);
    3. int index = Arrays.binarySearch(str,0,2,"cd");
    4. System.out.println(index);
    5. 输出:
    6. 1

    5.数组排序算法

    5.1 冒泡排序

            冒泡排序过程总是将小的往前放,大的往后放,类似水中气泡上升动作。每一轮排序都将最大的数顺到了数组的后面。

            基本思想:

            对比相邻的两个元素值大小,满足条件就交换元素值位置,把较小的元素值移动到数组的前面。

    1. /**
    2. * 冒泡排序算法
    3. */
    4. public class BubbleSort {
    5. public static void main(String[] args) {
    6. int arrs[] = new int[]{63, 4, 24, 1, 3, 15};
    7. //i控制轮数
    8. for (int i = 1; i < arrs.length; i++) {
    9. //j控制每轮对比的元素个数
    10. for (int j = 0; j < arrs.length - i; j++) {
    11. if (arrs[j] > arrs[j + 1]) {
    12. int temp = arrs[j];
    13. arrs[j] = arrs[j + 1];
    14. arrs[j + 1] = temp;
    15. }
    16. }
    17. }
    18. System.out.print("{");
    19. for (int arr : arrs) {
    20. System.out.print(arr + ",");
    21. }
    22. System.out.println("}");
    23. }
    24. }
    25. 输出:
    26. {1,3,4,15,24,63,}

    5.2 直接选择排序

            直接选择排序比冒泡排序更快

            基本思想:

            将指定排序位置与其他数组元素分别对比,如满足条件就交换元素位置。每一轮从待排序的数据中选出最小(最大)的一个元素,顺序地放到已排好序的数列的最后,直到全部数据排完。

            用一个临时变量记录当前已知的最大数索引值,每次与这个最大数索引值对比,如果比这个临时变量大,则这个临时变量就是这个数。

    1. public class SelectSort {
    2. public static void main(String[] args) {
    3. int arrs[] = new int[]{63, 4, 24, 1, 3, 15};
    4. for (int i = 1; i < arrs.length; i++) {
    5. int index = 0;
    6. for (int j = 1; j <= arrs.length - i; j++) { //j=0已被index占有,所以j从1开始;每轮最后一个数也要比较,所以j<=
    7. if (arrs[j] > arrs[index]) {
    8. index = j;
    9. }
    10. }
    11. int temp = arrs[arrs.length - i]; //把每轮的最后一个元素保存在临时变量中
    12. arrs[arrs.length - i] = arrs[index]; //把每轮已比较出的最大索引的值赋值给每轮的最后一个数
    13. arrs[index] = temp; //将每轮最后一个数与最大值数交换位置
    14. }
    15. System.out.print("{");
    16. for (int arr : arrs) {
    17. System.out.print(arr + ",");
    18. }
    19. System.out.print("}");
    20. }
    21. }
    22. 输出:
    23. {1,3,4,15,24,63,}

    5.3 反转排序

            反转排序就是把原有的数组内容以相反的顺序重新排列。

            基本思想:

            把数组最后一个元素与第一个元素交换位置,倒数第二个元素与第二个元素交换位置,以此类推。

    1. public class ReverseSort {
    2. public static void main(String[] args) {
    3. int arrs[] = new int[]{1, 2, 3, 4, 5, 6};
    4. for (int i = 0; i < arrs.length / 2; i++) {
    5. int temp = arrs[i];
    6. arrs[i] = arrs[arrs.length - i - 1];
    7. arrs[arrs.length - i - 1] = temp;
    8. }
    9. System.out.print("{");
    10. for (int arr : arrs) {
    11. System.out.print(arr + ",");
    12. }
    13. System.out.print("}");
    14. }
    15. }
    16. 输出:
    17. {6,5,4,3,2,1,}
  • 相关阅读:
    30岁从事软件测试,目前已失业4个月,迷茫不知该怎么办。?
    动态规划之空间压缩技巧
    基于electron+vue+element构建项目模板之【自定义标题栏&右键菜单项篇】
    Java 序列化和反序列化为什么要实现 Serializable 接口?
    RK3399驱动开发 | 09 - 基于RK808 PMIC的电源管理驱动
    cesium api放大缩小地图
    动态规划:13目标和
    基于langchain的开源大模型应用开发1
    C++11 Forward
    docker-compose部署Nacos集群
  • 原文地址:https://blog.csdn.net/qq_40132294/article/details/133773121