• 04、Java数组


    一、数组概述

    1、数组的定义

    数组是相同类型数据的有序集合,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们

    二、数组声明创建

    1、语法

    /*定义*/
    dataType[] arrayRefVar;	
    //或
    dataType arrayRefVat[];
    /*创建*/
    dataType[] arrayRefVat = new dataType[arraysize];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、例

    package com.s1lu.array;
    
    /**
     * @Author S1Lu
     * @create 2022/10/29 20:42
     */
    public class Demo01 {
        public static void main(String[] args) {
            int[] nums; //1、定义
            //或者
            //int nums[];
            nums = new int[5]; //2、定义数组的长度为5
    
            //3、赋值
            nums[0] = 1;
            nums[1] = 1;
            nums[2] = 2;
            nums[3] = 3;
            nums[4] = 4;
    
    
            //计算总和
            int s = 0;
            for(int i =0;i<nums.length;i++){
                s += nums[i];
            }
            System.out.println(s);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    三、三种初始化以及内存分析

    1、java内存分析

    2、数组的三种初始化

    • 静态初始化
    • 动态初始化
    • 默认初始化
     package com.s1lu.array;
    
    /**
     * @Author S1Lu
     * @create 2022/11/8 17:02
     */
    public class Demo02 {
        public static void main(String[] args) {
            //静态初始化 :创建 + 赋值
            int [] a = {1,2,3,4,5,6};
            System.out.println(a);
    
            //动态初始化:包含默认初始化
            int[] b = new int[10];
            b[0] = 10;
            System.out.println(b[0]);
    
            //默认初始化 :int类型默认为0
            System.out.println(b[1]);
            System.out.println(b[2]);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    四、数组边界

    下标的合法区间为:[0,length-1],越界报错java.lang.ArrayIndexOutOfBoundsException

    五、数组使用

    1、for

    package com.s1lu.array;
    
    /**
     * @Author S1Lu
     * @create 2022/11/8 17:14
     */
    public class Demo03 {
        public static void main(String[] args) {
            int[] arrays = {1,2,3,4,5}; 
            for (int i=0;i<arrays.length;i++){
                System.out.println(arrays[i]);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2、for -each

    package com.s1lu.array;
    
    /**
     * @Author S1Lu
     * @create 2022/11/8 17:14
     */
    public class Demo03 {
        public static void main(String[] args) {
            int[] arrays = {1,2,3,4,5};
    
            for (int array :arrays){
                System.out.println(array);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    六、多维数组

    • 二维数组
    package com.s1lu.array;
    
    /**
     * @Author S1Lu
     * @create 2022/11/11 10:03
     */
    public class Demo04 {
        public static void main(String[] args) {
            int [][] array= {{1,2},{2,3},{3,4}};
            for (int i =0 ; i < array.length ; i ++ ){
                for (int j =0 ; j < array[i].length ; j ++ ) {
                    System.out.println(array[i][j]);
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 多维数组

    七、Arrays类

    其他的查看JDK帮助文档

    1、Arrays.toString

    package com.s1lu.array;
    
    import java.util.Arrays;
    
    /**
     * @Author S1Lu
     * @create 2022/11/11 10:07
     */
    public class Demo05 {
        public static void main(String[] args) {
            int[] a ={1,2,3,4,5,6};
    
            System.out.println(a);  //   [I@1540e19d
    
            System.out.println(Arrays.toString(a)); //[1, 2, 3, 4, 5, 6]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2、Arrays.sort

    package com.s1lu.array;
    
    import java.util.Arrays;
    
    /**
     * @Author S1Lu
     * @create 2022/11/11 10:15
     */
    public class Demo06 {
        public static void main(String[] args) {
            int[] a ={1,3,4,2,11,6};
    
            Arrays.sort(a); //对数组排序
    
            System.out.println(Arrays.toString(a)); //[1, 2, 3, 4, 6, 11]
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    八、稀疏数组

    当-个数组中大部分元素为0,或者为同一值的数组时,可以使用稀疏数组来保存该数组。

    稀疏数组的处理方式是:

    • 记录数组-共有几行几列,有多少个不同值
    • 把具有不同值的元素和行列及值记录在一一个小规模的数组中,从而缩小程序的规模

  • 相关阅读:
    Java Web概述
    浏览器交互:Cookies、事件、浏览历史
    SPA项目搭建及嵌套路由
    OOM内存溢出分析
    C++左值/右值、左值引用&/右值引用&&、移动语义move、完美转发forward
    开源轻量堡垒机——Teleport的安装配置和使用
    365天挑战LeetCode1000题——Day 080 寻找重复的子树 岛屿的最大面积 统计子岛屿
    Hadoop学习记录3--HDFS知识补充
    Vue图片URL转File实践[已解决跨域问题]
    Python urllib
  • 原文地址:https://blog.csdn.net/qq_51644623/article/details/127801987