zoukankan      html  css  js  c++  java
  • Java基础数组

    一、概念

    1. 数组是多个相同数据类型按一定顺序排列的集合,使用一个名字命名,并通过编号的方式对这些数据进行统一管理。

    2. 数组本身是引用数据类型,而数组中的元素可以是任意数据类型,包括基本数据类型和引用数据类型。

    3. 创建数组对象会在内容中开辟一块连续的内存空间,而数组名中引用的是这块连续空间的首地址。

    4. 数组的长度一旦确定,就不能修改。

    5. 数据分类

      1. 按维度分:一维数组、二维数组、...
      2. 按元素类型分:基本数据类型元素的数组、引用数据类型元素的数组

    二、一维数组

    1. 声明和初始化

    public class ArrayTest {
        public static void main(String[] args) {
            // 1. 静态初始化:数组的初始化和数组元素的赋值操作同时进行
            int[] ids; // 声明
            ids = new int[]{1001, 1002, 1003, 1004};
    
            // 2. 动态初始化:数组的初始化和数组元素的赋值操作分开进行
            String[] names;
            names = new String[5];
    
            // 声明 + 初始化
            String[] animals = new String[10];
            char[] grades = new char[]{'A', 'B', 'C', 'D'};
            
            // 其他写法
            int[] nums = {1, 2, 3, 4}; // 类型推断
        }
    }
    

    2. 数组索引

    1. 通过索引的方式调用数据指定位置的元素。
    2. 数组的索引从0开始,到数组长度-1结束。
    package com.example.www;
    
    public class ArrayTest {
        public static void main(String[] args) {
            String[] names;
            names = new String[5];
    
            names[0] = "关羽";
            names[1] = "张飞";
            names[2] = "黄忠";
            names[3] = "马超";
            names[4] = "赵云";
        }
    }
    

    3. 数组长度

    public class ArrayTest {
        public static void main(String[] args) {
            String[] names;
            names = new String[5];
            System.out.println(names.length); // 5
        }
    }
    

    4. 数组遍历

    public class ArrayTest {
        public static void main(String[] args) {
            String[] names;
            names = new String[5];
    
            names[0] = "关羽";
            names[1] = "张飞";
            names[2] = "黄忠";
            names[3] = "马超";
            names[4] = "赵云";
    
            for (int i = 0; i < names.length; i++) {
                System.out.println(names[i]);
            }
        }
    }
    

    5. 默认初始化值

    public class ArrayTest {
        public static void main(String[] args) {
            String[] names;
            names = new String[5];
            for (int i = 0; i < names.length; i++) {
                System.out.println(names[i]); // null
            }
        }
    }
    

    数组元素是整型:0

    数组元素是浮点型:0.0

    数组元素是char型:ASCII中的0(空字符)

    数组元素是boolean型:false

    数组元素是引用数据类型:null

    6. 数组内存解析

    三、二维数组

    1. 声明和初始化

    public class ArrayTest {
        public static void main(String[] args) {
           // 1. 静态初始化
           int[][] arr1 = new int[][]{{1,2,3},{4,5},{6}};
           // 2. 动态初始化
           int[][] arr2 = new int[3][];
           int[][] arr3 = new int[3][2];
    
           // 其他写法
            int arr4[][] =  new int[3][];
            int[] arr5[] = new int[3][];
            int[][] arr6 = {{1,2,3},{4,5},{6}};
        }
    }
    

    2. 数组索引

    public class ArrayTest {
        public static void main(String[] args) {
            int[][] arr1 = new int[][]{{1,2,3},{4,5},{6}};
            System.out.println(arr1[0][1]); // 2
            int[][] arr2 = new int[3][];
            arr2[1] = new int[4];
            System.out.println(arr2[1][0]); // 0
        }
    }
    

    3. 数组长度

    public class ArrayTest {
        public static void main(String[] args) {
            int[][] arr1 = new int[][]{{1,2,3},{4,5},{6}};
            System.out.println(arr1.length); // 3
            System.out.println(arr1[1].length); // 2
        }
    }
    

    4. 数组遍历

    public class ArrayTest {
        public static void main(String[] args) {
            int[][] arr = new int[][]{{1,2,3},{4,5},{6}};
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr[i].length; j++) {
                    System.out.print(arr[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
    //1 2 3
    //4 5
    //6 
    

    5. 默认初始化值

    public class ArrayTest {
        public static void main(String[] args){
            // 初始化方式一:
            int[][] arr1 = new int[4][3];
            System.out.println(arr1[0]); // [I@1b6d3586 地址值
            System.out.println(arr1[0][0]); // 0
            System.out.println(arr1); // [[I@4554617c
            
    		// 初始化方式二:
            int[][] arr2 = new int[4][];
            System.out.println(arr2[0]); // null
            System.out.println(arr2[0][1]); // NullPointerException, 不能调用,报错
        }
    }
    

    6. 数组内存解析

    四、Arrays工具类

    import java.util.Arrays;
    
    public class ArrayTest {
        public static void main(String[] args) {
            int[] arr1 = new int[]{1, 2, 3, 4};
            int[] arr2 = new int[]{2, 1, 4, 3};
            boolean isEquals = Arrays.equals(arr1,arr2);
            // 判断数组是否相等
            System.out.println(isEquals); // false
            // 输出数组信息
            System.out.println(Arrays.toString(arr1)); // [1, 2, 3, 4]
            // 将指定值填充到数组中
            Arrays.fill(arr1, 10);
            System.out.println(Arrays.toString(arr1)); // [10, 10, 10, 10]
            // 排序
            Arrays.sort(arr2);
            System.out.println(Arrays.toString(arr2)); // [1, 2, 3, 4]
            // 二分查找
            int[] arr3 = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 21, 23};
            int index = Arrays.binarySearch(arr3, 17);
            System.out.println(index); // 6
        }
    }
    
    

    五、数组练习

    1. 杨辉三角

    // 考察元素赋值
    
    public class YangHuiTest {
        public static void main(String[] args) {
            // 1. 声明并初始化二维数组
            int[][] yangHui = new int[10][];
            // 2. 给数组的元素赋值
            for (int i = 0; i < yangHui.length; i++) {
                yangHui[i] = new int[i + 1];
                // 2.1 先给首末元素赋值
                yangHui[i][0] = yangHui[i][i] = 1;
                // 2.2 给每行的非首末元素赋值
                if (i > 1) {
                    for (int j = 1; j < yangHui[i].length - 1; j++) {
                        yangHui[i][j] = yangHui[i-1][j-1] + yangHui[i-1][j];
                    }
                }
            }
            // 3. 遍历二维数组
            for (int i = 0; i < yangHui.length; i++) {
                for (int j = 0; j < yangHui[i].length; j++) {
                    System.out.print(yangHui[i][j] + " ");
                }
                System.out.println();
            }
        }
    }
    

    2. 求数值型数组中元素的最大值、最小值、平均数和总和

    定义一个int型的一维数组,包含10个元素,分别赋予随机两位整数。然后求出所有元素的最大值、最小值、平均数、和,并输出。

    //获取[a,b]范围的随机数:(int)(Math.random()*(b-a+1)+a)
    
    public class ArrayTest {
        public static void main(String[] args) {
            int[] arr = new int[10];
    
            for (int i = 0; i < arr.length; i++) {
                arr[i] = (int) (Math.random() * (99 - 10 + 1) + 10);
            }
    
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
            System.out.println();
            //最大值
            int maxValue = arr[0];
            for (int i = 1; i < arr.length; i++) {
                if (maxValue < arr[i]) {
                    maxValue = arr[i];
                }
            }
            System.out.println("最大值为:" + maxValue);
            //最小值
            int minValue = arr[0];
            for (int i = 1; i < arr.length; i++) {
                if (minValue > arr[i]) {
                    minValue = arr[i];
                }
            }
            System.out.println("最小值为:" + minValue);
            //求和
            int sum = 0;
            for (int i = 0; i < arr.length; i++) {
                sum += arr[i];
            }
            System.out.println("和:" + sum);
            //平均数
            int avgValue = sum / arr.length;
            System.out.println("平均数为:" + avgValue);
        }
    }
    

    3. 数组复制

    public class ArrayTest {
        public static void main(String[] args) {
            int[] arr1,arr2;
            arr1 = new int[]{2, 3, 5, 7, 11, 13, 17, 19};
            // 显示arr1的内容
            for (int i = 0; i < arr1.length; i++) {
                System.out.print(arr1[i] + " "); // 2 3 5 7 11 13 17 19
            }
            System.out.println();
            // 1.赋值arr2变量等于arr1
            // 不能称作数组的复制,arr1和arr2地址值相同
            // arr2 = arr1;
    
            // 2. 遍历赋值
            arr2 = new int[arr1.length];
            for (int i = 0; i < arr1.length; i++) {
                arr2[i] = arr1[i];
            }
            // 修改arr2中的元素
            for (int i = 0; i < arr2.length; i++) {
                if (i % 2 == 0){
                    arr2[i] = i;
                }
            }
            // 显示arr1的内容
            for (int i = 0; i < arr1.length; i++) {
                System.out.print(arr1[i] + " "); // 2 3 5 7 11 13 17 19
            }
            System.out.println();
            // 显示arr2的内容
            for (int i = 0; i < arr2.length; i++) {
                System.out.print(arr2[i] + " "); // 0 3 2 7 4 13 6 19
            }
            System.out.println();
    
        }
    }
    

    4. 数组反转

    public class ArrayTest {
        public static void main(String[] args) {
    
            int[] arr = new int[]{2, 3, 5, 7, 11, 13, 17, 19};
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " "); // 2 3 5 7 11 13 17 19
            }
            System.out.println();
    
            // 数组反转
            // 方法一
    //        for (int i = 0; i < arr.length / 2; i++) {
    //            int tmp = arr[i];
    //            arr[i] = arr[arr.length - i - 1];
    //            arr[arr.length - i - 1] = tmp;
    //        }
            
            // 方法二
            for (int i = 0, j = arr.length - 1; i<j;i++,j--) {
                int tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
    
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " "); // 19 17 13 11 7 5 3 2
            }
    
        }
    }
    

    5. 查找

    5.1 线性查找

    public class SearchTest {
        public static void main(String[] args) {
            int[] arr = new int[]{2, 3, 5, 7, 11, 13, 17, 19};
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " "); // 2 3 5 7 11 13 17 19
            }
            System.out.println();
    
            int dest = 17;
            // 线性查找
            for (int i = 0; i < arr.length; i++) {
                if (dest == arr[i]){
                    System.out.println("Index:"+i);
                    break;
                }
            }
        }
    }
    

    5.2 二分法

    前提:所要查找的数组必须有序

    public class SearchTest {
        public static void main(String[] args) {
            int[] arr = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 21, 23};
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " "); // 2 3 5 7 11 13 17 19
            }
            System.out.println();
    
            int dest = 17;
            // 二分法查找
            int head = 0; // 初始的首索引
            int end = arr.length - 1; // 初始的末索引
            boolean isFlag = true;
            while (head <= end){
                int middle = (head + end) / 2;
                if (dest == arr[middle]){
                    System.out.println("Index:" + middle);
                    isFlag = false;
                    break;
                }else if (arr[middle] > dest) {
                    end = middle -1 ;
                }else {
                    head = middle + 1;
                }
            }
            if(isFlag){
                System.out.println("Not found");
            }
        }
    }
    

    6. 排序

    6.1 冒泡排序

    public class BubbleSortTest {
        public static void main(String[] args) {
            int[] arr = new int[]{11, 17, 2, 13, 5, 23, 7, 3};
            // 冒泡排序
            for (int i = 0; i < arr.length - 1; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    if (arr[j] > arr[j+1]) {
                        int tmp = arr[j];
                        arr[j] = arr[j+1];
                        arr[j+1] = tmp;
                    }
                }
            }
    
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " "); // 2 3 5 7 11 13 17 23
            }
        }
    }
    
  • 相关阅读:
    ubuntu14.04server下安装scala+sbt工具
    如何在Ubuntu server中修改IP
    机器学习涉及到的数学知识
    Openfire+spark在linux上搭建内部聊天系统
    ubuntu14.04server版安装redis
    网站流量统计
    Visual Studio-使用vs2015 调用 vs2010编译的库时解决"无法解析的外部符号__iob_func 问题"
    书签中的一些工具整理
    android开发学习——day3
    android开发学习——day2
  • 原文地址:https://www.cnblogs.com/sufangmu/p/15171754.html
Copyright © 2011-2022 走看看