• 四、java 数组


    一、数组的理解:多个相同类型数据 按照 一定顺序排列 的集合,并使用同一个名字命名,通过编号的方式对这些数据进行统一管理
    1.数组是有序排列;
    2.数组属于引用数据类型的变量。数组的元素既可以是 基本数据类型,也可以使 引用数据类型;
    3.创界数组对象会在内存中会开辟一整串联系空间;
    4.数组的长度:元素的个数,长度一旦确定就不能修改;
     
    一维数组的使用
      1.一维数组的声明和初始化;
      2.如何调用数据的指定位置元素;
      3.如何获取数组长度;
      4.如何遍历数组;
      5.数组元素的默认初始化值;
      6.数组的内存解析;
     

     1 public static void oneDimesion() {
     2         //1.数组的声明  1。1静态初始化
     3         int[] ids = new int[]{1001,1002,1003};
     4         //数组的声明  1.2动态初始化
     5         String[] name = new String[4];
     6         //总结:一旦数组初始化成功,其长度就确定了;
     7         
     8         //2.如何调用数组的指定位置元素:通过索引的方式调用,索引从0开始;到 长度-1 结束;
     9         name[0] = "joy";
    10         name[1] = "tom";
    11         name[2] = "lucy";
    12         name[3] = "jreey";
    13         
    14         //3数组长度
    15         System.out.println(name.length);
    16         
    17         //4数组的遍历
    18         for(int i=0;i<name.length;i++) {
    19             System.out.println(name[i]);
    20         }
    21         
    22         //数组的默认初始化值,整型数组默认初始化值 0;char:0 ;boolean:false;  浮点型:0.0;引用数据类型:null
    23         int[] arr = new int[4];
    24         for(int i=0;i<arr.length;i++) {
    25             System.out.println(arr[i]);
    26         }
    27         
    28         char[] arr1 = new char[4];
    29         for(int i=0;i<arr1.length;i++) {
    30             System.out.println("char 默认初始化值:"+arr1[i]);
    31         }
    32         
    33         boolean[] arr2 = new boolean[4];
    34         for(int i=0;i<arr2.length;i++) {
    35             System.out.println("Boolean 默认初始化值:"+arr2[i]);
    36         }
    37         
    38         String[] arr3 = new String[4];
    39         for(int i=0;i<arr3.length;i++) {
    40             System.out.println("引用数据类型 默认初始化值:"+arr3[i]);
    41         }
    42     }

    一维数组的内存解析:

     二维数组的理解:我们可以看成时一维数组array1 又作为 array2的元素存在;
     1.数组是有序排列;
     2.数组属于引用数据类型的变量。数组的元素既可以是 基本数据类型,也可以使 引用数据类型;
     3.创界数组对象会在内存中会开辟一整串联系空间;
     4.数组的长度:元素的个数,长度一旦确定就不能修改;
     
      二维数组的使用

      1.二维数组的声明和初始化;
      2.如何调用数据的指定位置元素;
      3.如何获取数组长度;
      4.如何遍历数组;
      5.数组元素的默认初始化值;
      6.数组的内存解析;
     
      数组的默认初始化值:(外层元素:如int[3];内层元素:如 int[3][2])
      针对初始化方式一:如:int[][] arr = new int[4][3];
      外层元素的初始化值:地址值;
      内层元素的初始化值为:与一位数组初始化情况相同;
      针对初始化方式二:如:int[][] arr = new int[4][];
      外层元素的初始化值:null
      内层元素的初始化值为:内层元素不能调用;

     1 public static void testMultidimession() {
     2         // 1.数组的声明 1。1静态初始化
     3         int[] ids = new int[] { 1001, 1002, 1003 };
     4         int[][] ids1 = new int[][]{{ 1001, 1002, 1003 },{ 1001, 100, 1003 },{ 1001, 99, 1003 }};
     5         
     6         int[][] ids2 = new int[4][];
     7         // 数组的声明 1.2动态初始化
     8         String[] name = new String[4];
     9         String[][] name1 = new String[4][3];//4行3列
    10         String[][] name2 = new String[4][];//4行 列数不确定,如果列数不一样,就用这种方式初始化
    11 
    12         // 2.如何输出指定位置的元素;
    13         System.out.println(ids1[0][1]);
    14         System.out.println(name1[1][1]);
    15         //System.out.println(name2[1][1]);//报错,空指针异常
    16 
    17         // 3数组长度
    18         System.out.println(ids1.length);
    19         // 4数组的遍历
    20         for (int i = 0; i < ids1.length; i++) {
    21             for(int j=0;j<ids1[i].length;j++) {
    22                 System.out.print(ids1[i][j]+" ");
    23             }
    24             System.out.print("
    ");
    25         }
    26 
    27         // 数组的默认初始化值,整型数组默认初始化值 0;char:0 ;boolean:false; 浮点型:0.0;引用数据类型:null
    28         int[][] arr = new int[4][3];
    29         System.out.println("int 二维数组默认初始化:"+arr[0]);//地址值[I@6d06d69c
    30         System.out.println("int 二维数组默认初始化:"+arr[0][0]);//0
    31         System.out.println("int 二维数组默认初始化:"+arr);////地址值[[I@7852e922
    32         
    33         System.out.println("string 二维数组 列数不确定:"+name2);//[[Ljava.lang.String;@4e25154f
    34         System.out.println("string 二维数组 列数不确定:"+name2[0]);//null
    35         System.out.println("int 二维数组 列数不确定:"+ids2[0]);//null
    36         
    37         String[] str1 = {"2","3"};
    38         name2[0] = str1;
    39         System.out.println(name2[0][1]);//null
    40     }

    三、数组的常用操作

      1 /**
      2      * 
      3      * @Description 获取arr数组中所有元素的和
      4      * @author lixiuming
      5      * @date 2020年5月5日下午12:32:08
      6      *
      7      */
      8     public static void multiDimessionAdd() {
      9         int[][] arr = new int[][] { { 3, 5, 8 }, { 12, 9 }, { 7, 0, 6, 4 } };
     10         int sum = 0;
     11         for (int i = 0; i < arr.length; i++) {
     12             for (int j = 0; j < arr[i].length; j++) {
     13                 sum += arr[i][j];
     14 
     15             }
     16         }
     17         System.out.println(sum);
     18     }
     19 
     20     /**
     21      * 
     22      * @Description 打印10行杨辉三角
     23      * @author lixiuming
     24      * @date 2020年5月5日下午12:42:46
     25      *
     26      */
     27     public static void multiDimessionYangHui() {
     28         int[][] yangHui = new int[10][];
     29         for (int i = 0; i < 10; i++) {
     30             int[] arr = new int[i + 1];
     31             for (int j = 0; j <= i; j++) {
     32                 if (j == 0 || j == i) {
     33                     arr[j] = 1;
     34                     yangHui[i] = arr;
     35                     System.out.print(1 + "	");
     36                 } else {
     37                     yangHui[i][j] = yangHui[i - 1][j - 1] + yangHui[i - 1][j];
     38                     System.out.print(yangHui[i][j] + "	");
     39                 }
     40 
     41             }
     42             System.out.println();
     43         }
     44 
     45         // 不放心可以重新遍历元素
     46         for (int i = 0; i < yangHui.length; i++) {
     47             for (int j = 0; j < yangHui[i].length; j++) {
     48                 System.out.print(yangHui[i][j] + "	");
     49             }
     50             System.out.println();
     51         }
     52     }
     53 
     54     /**
     55      * 
     56      * @Description        创建一个长度为6的int型数组,要求数组的值在1-30之间,且是随机赋值,同时,要求元素的值各不同;
     57      * @author            lixiuming
     58      * @date            2020年5月6日上午10:09:46
     59      *
     60      */
     61     public static void testArithmetic1() {
     62         int[] arr = new int[6];
     63         Random random = new Random();
     64         for(int i = 0;i<arr.length;i++) {
     65             int radNum = random.nextInt(30)+1;
     66                 for(int j = 0;j<i && i > 0;j++) {
     67                     while(arr[j] - radNum ==0) {
     68                         radNum = random.nextInt(30)+1;
     69                     }
     70                 }
     71                 arr[i] = radNum;
     72         }
     73         for(int i = 0;i<arr.length;i++) {
     74             System.out.println("1-30 6为数组的随机数:"+arr[i]+"	");
     75         }
     76         
     77     }
     78     
     79     /**
     80      * 
     81      * 求数值型数组中元素的最大值,最小值,平均数,总和
     82      * <p>随机数公式 [a,b] Math.random()*(b-a+1)+a;
     83      * @Description        求数值型数组中元素的最大值,最小值,平均数,总和
     84      *                     定义int型数组,包含10个元素,分别赋两位随机数求最大值,最小值,平均数,总和
     85      * @author            lixiuming
     86      * @date            2020年5月6日下午2:15:08
     87      *
     88      */
     89     public static void testArithmetic2() {
     90         int[] arr = new int[10];
     91         for(int i = 0; i<10;i++) {
     92             Random random = new Random();
     93             arr[i] = random.nextInt(90)+10;
     94         }
     95         
     96         int sum = 0;
     97         int avg = 0;
     98         int min = 0;//最好直接指定值 即 arr[0]
     99         int max = 0;//最好直接指定值 即 arr[0]
    100         for(int i = 0; i<arr.length;i++) {
    101             sum += arr[i];
    102         }
    103         System.out.println("总和:"+sum);
    104         System.out.println("平均:"+(sum/10.0));
    105         
    106         for(int i = 0; i<arr.length;i++) {
    107             System.out.print(arr[i]+"	");
    108             min = arr[0];
    109                 if(arr[i]>=max) {
    110                     max = arr[i];
    111                 }
    112                 if(arr[i]<=min) {
    113                     min = arr[i];
    114                 }
    115         }
    116         System.out.println();
    117         System.out.println("最大:"+max);
    118         System.out.println("最小:"+min);
    119     }
    120 
    121     /**
    122      * 
    123      * 使用简单数组 数组复制
    124      * 
    125      * <p>
    126      * 1.创建两个int[]了星星变量 arr1,arr2;</br>
    127      * 2.使用{},把arr1初始化8个素数;</br>
    128      * 3.显示arr1内容;</br>
    129      * 4.arr2 = arr1,修改arr2的偶素索引元素,使他等于索引值 即 arr2[0] = 0;;arr[2] == 2;</br>
    130      * 
    131      * @Description        
    132      * @author            lixiuming
    133      * @date            2020年5月6日下午3:13:28
    134      *
    135      */
    136     public static void testArithmetic3() {
    137         int[] arr1,arr2;
    138         arr1 = new int[]{2,3,5,7,11,13,17,19};
    139         arr2 = arr1;//这个操作不能复制数组;arr1 arr2 都指向了堆空间的唯一一个数组实体;
    140         for(int i=0; i<arr2.length;i++) {
    141             System.out.print("原始:"+arr1[i]+"	");
    142         }
    143         for(int i=0; i<arr2.length;i++) {
    144             if(i%2 == 0) {
    145                 arr2[i] = i;
    146             }
    147         }
    148         System.out.println();
    149         for(int i=0; i<arr1.length;i++) {
    150             System.out.print("修改arr2后arr1的结果:"+arr1[i]+"	");
    151         }
    152         
    153         //数组复制
    154         arr1 = new int[]{2,3,5,7,11,13,17,19};
    155         arr2 = new int[arr1.length];
    156         System.out.println();
    157         for(int i=0; i<arr1.length;i++) {
    158             if(i%2 == 0) {
    159                 arr2[i] = i;
    160             }else {
    161                 arr2[i] = arr1[i];
    162             }
    163         }
    164         for(int i=0; i<arr1.length;i++) {
    165             System.out.print("(重新new)修改arr2后arr1的结果:"+arr1[i]+"	");
    166         }
    167     }
    168     /**
    169      * 数组反转
    170      * @Description        
    171      * @author            lixiuming
    172      * @date            2020年5月7日下午12:45:29
    173      *
    174      */
    175     public static void testArithmetic4() {
    176         int[] arr1 = new int[]{2,3,5,7,11,13,17,19};
    177         int[] arr2 = new int[arr1.length];
    178         for(int i=0;i<arr1.length;i++) {
    179             arr2[i] = arr1[i];
    180         }
    181         for(int i=(arr2.length-1);i>=0;i--) {
    182             int j = arr2.length-1-i;
    183             arr1[j] = arr2[i];
    184         }
    185         System.out.println();
    186         for(int i=0;i<arr1.length;i++) {
    187             System.out.print(arr1[i]+"	");
    188         }
    189         
    190 
    191         //推荐 :写法二
    192         arr1 = new int[]{2,3,5,7,11,13,17,19};
    193         for(int i = 0;i<arr1.length/2;i++) {
    194             int temp = arr1[i];
    195             arr1[i] = arr1[arr1.length-1-i];
    196              arr1[arr1.length-1-i] = temp;
    197         }
    198         System.out.println();
    199         for(int i=0;i<arr1.length;i++) {
    200             System.out.print(arr1[i]+"	");
    201         }
    202     }
    203     
    204     /**
    205      * 数组线性查找
    206      * @Description        
    207      * @author            lixiuming
    208      * @date            2020年5月7日下午3:05:21
    209      *
    210      */
    211     public static void testArithmetic5() {
    212         String[] arr1 = new String[] {"AA","BB","CC","DD"};
    213         String dest = "BB";
    214         boolean flag = false;
    215         for(int i = 0; i<arr1.length;i++) {
    216             if(dest.equals(arr1[i])) {
    217                 flag = true;
    218                 System.out.println("找到了指定元素,位置为:"+i);
    219                 break;
    220             }
    221         }
    222         if(!flag) {
    223             System.out.println("没找到指定元素");
    224         }
    225     }
    226     
    227     /**
    228      * 二分法查找 
    229      * 
    230      * <p>所要查找的数组必须有序
    231      * @Description        
    232      * @author            lixiuming
    233      * @date            2020年5月7日下午3:05:21
    234      *
    235      */
    236     public static void testArithmetic6() {
    237         int[] arr1 = new int[] {-98,-34,2,34,54,66,79,105,210,333};
    238         int dest = 2;
    239         int start = 0;
    240         int end = arr1.length-1;
    241         boolean flag = false;
    242         for(int i = start; i<end;i++) {
    243             int indexNum = (end + start)/2;
    244             if(dest<arr1[indexNum]) {
    245                 end = indexNum-1;
    246             }else if(dest>arr1[indexNum]){
    247                 start = indexNum+1;
    248             }else if(dest == arr1[indexNum]){
    249                 flag = true;
    250                 System.out.println("找到了指定元素,位置为:"+indexNum);
    251                 break;
    252             }
    253         }
    254         if(!flag) {
    255             System.out.println("没找到指定元素");
    256         }
    257     }
    258     /**
    259      * 数组的冒泡排序
    260      * @Description        
    261      * @author            lixiuming
    262      * @date            2020年5月8日上午11:40:06
    263      *
    264      */
    265     public static void testArithmetic7() {
    266         int[] arr = new int[] {43,32,76,98,0,64,33,-21,32,99};
    267 //        冒泡排序 从小到大排序
    268         for(int i = 0;i<arr.length-1;i++) {//大轮
    269             for(int j = 0;j<arr.length-1-i;j++) { //小轮
    270                 if(arr[j]>arr[(j+1)]) {
    271                     int temp = arr[j];
    272                     arr[j] = arr[j+1];
    273                     arr[j+1] = temp;
    274                 }
    275             }
    276             
    277         }
    278         
    279         System.out.println();
    280         for(int i = 0;i<arr.length;i++) {
    281             System.out.print(arr[i] +"	");
    282         }
    283         System.out.println();
    284     }

    四、Arrays工具类的使用:java.util.Arrays:操作数组工具类,里面定义了很多操作数组的方法

     1 /**
     2      * 比较两个数组是否相等;
     3      * 
     4      * @Description
     5      * @author lixiuming
     6      * @date 2020年5月8日下午3:50:17
     7      *
     8      */
     9     public static void testEquals() {
    10         int[] arr1 = new int[] { 1, 2, 3, 4 };
    11         int[] arr2 = new int[] { 1, 3, 2, 4 };
    12         boolean res = Arrays.equals(arr1, arr2);
    13         System.out.println(res);
    14     }
    15 
    16     /**
    17      * 
    18      * @Description
    19      * @author lixiuming
    20      * @date 2020年5月8日下午3:56:38
    21      *
    22      */
    23     public static void testToString() {
    24         int[] arr1 = new int[] { 1, 2, 3, 4 };
    25         System.out.println(Arrays.toString(arr1));
    26     }
    27 
    28     /**
    29      * 将指定的值填充到数组中
    30      * 
    31      * @Description
    32      * @author lixiuming
    33      * @date 2020年5月13日上午10:04:56
    34      *
    35      */
    36     public static void testFill() {
    37         int[] arr1 = new int[] { 1, 2, 3, 4 };
    38         Arrays.fill(arr1, 10);// 填充所有
    39         Arrays.fill(arr1, 0, 1, 1);// 填充到指定位置
    40         System.out.println(Arrays.toString(arr1));
    41     }
    42 
    43     /**
    44      * 排序
    45      * 
    46      * @Description
    47      * @author lixiuming
    48      * @date 2020年5月13日上午10:11:37
    49      *
    50      */
    51     public static void testSort() {
    52         int[] arr1 = new int[] { 1, 3, 5, 4 };
    53         Arrays.sort(arr1);
    54         System.out.println(Arrays.toString(arr1));
    55 
    56         String[] str1 = new String[] { "b", "e", "a", "c" };
    57         Arrays.sort(str1);
    58         System.out.println(Arrays.toString(str1));
    59     }
    60 
    61     /**
    62      * 二分查找
    63      * 
    64      * @Description
    65      * @author lixiuming
    66      * @date 2020年5月13日上午10:12:08
    67      *
    68      */
    69     public static void testBinarySearch() {
    70         int[] arr1 = new int[] { 1, 2, 3, 4,50,51,52,68,100 };
    71         int res = Arrays.binarySearch(arr1,52);
    72         System.out.println(res);
    73     }
  • 相关阅读:
    【深度学习】吴恩达网易公开课练习(class1 week2)
    【深度学习】吴恩达网易公开课练习(class1 week3)
    【python】内存调试
    【python】threadpool的内存占用问题
    Druid: A Real-time Analytical Data Store
    Mesa: GeoReplicated, Near RealTime, Scalable Data Warehousing
    Presto: SQL on Everything
    The Snowflake Elastic Data Warehouse
    Guava 库
    Java Annotation
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/12888380.html
Copyright © 2020-2023  润新知