数组

数组的定义

  • 数组是相同类型数据的有序集合。
  • 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成。
  • 其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问它们。

数组的声明和创造

    public static void main(String[] args) {
      //首先必须声明数组变量,才能在程序中使用数组。
        int[] nums;//1.声明一个数组
        int nums1[];//2.也可,但推荐第一种。
      
      //Java语言使用new操作符来创建数组,语法如下:
        nums = new int[10];//创建一个数组。(声明时并没有给数组分配空间,创建时才给数组分配空间。)    这里可存放10个int类型的数字
        
      //可将声明和创造一起进行
      //如 int[] nums = new int[10];
      
      //注意:数组的元素是通过索引访问的,数组索引从0开始。
      
        //3.给数组元素中赋值
        nums[0] = 1;
        nums[1] = 2;
        nums[2] = 3;
        nums[3] = 4;
        nums[4] = 5;
        nums[5] = 6;
        nums[6] = 7;
        nums[7] = 8;
        nums[8] = 9;
        nums[9] = 10;

        //计算所有元素的和
        int sum = 0;
        //获取数组长度:arrays.length

        for (int i = 0; i < nums.length; i++) {
            sum = sum + nums[i];
        }

        System.out.println("总和为:" + sum);


    }

/*
总和为:55

Process finished with exit code 0
*/

三种初始化

  • 静态初始化
//静态初始化: 创建 + 赋值
        int[] a = {1,2,3,4,5,6,7,8};

        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
/*
1
2
3
4
5
6
7
8

Process finished with exit code 0
*/
  • 动态初始化
//动态初始化: 包含默认初始化
        int[] b = new int[10];
        b[0] = 10;
        b[1] = 9;

        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);
        }

/*
10
9
0   //没赋值,这里默认为0
0
0
0
0
0
0
0

Process finished with exit code 0
*/
  • 默认初始化:

数组是引用类型,它的元素相当于类的示例变量,因此数组一经分配空间,其中的每个元素也被按照示例变量同样的方式被隐试初始化。

数组的四个基本特点

  • 其长度是确定的。数组一旦被创建,它的大小就是不可以改变的。
  • 其元素必须是相同类型,不允许出现混合类型。
  • 数组中的元素可以是任何数据类型,包括基本类型和引用类型。
  • 数组变量属引用类型,数组也可以看成是对象,数组中的每个元素相当与该对象的成员变量。数组本身就是对象,Java中对象是在堆中的,因此数组无论保存原始类型还是其他对象类型,数组对象本身是在堆中的。

数组边界

  • 下标的合法区间:[0,length-1],如果越界就会报错;
public class ArrayDemo03 {
    public static void main(String[] args) {
        int[] a = new int[10];//数组长度为10,所以数组下标区间为[0,9]
        System.out.println(a[10]);//a[10],下标超出了边界。
    }
}
/*
这是运行后报的错:“ArrayIndexOutOfBoundsException” 意为:数组下标越界异常
*/

数组的使用

普通的for循环:
public class ArrayDemo04 {
    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]);
        }
        System.out.println("========================");
        //计算所有元素的和
        int sum = 0;
        for (int i = 0; i < arrays.length; i++) {
            sum += arrays[i];
        }
        System.out.println("sum=" + sum);
        System.out.println("========================");
        //查找最大元素
        int max = arrays[0];
        for (int i = 1; i < arrays.length; i++) {
            if (arrays[i]>max){
                max = arrays[i];
            }
        }

        System.out.println("max=" + max);
    }
}
/*
1
2
3
4
5
========================
sum=15
========================
max=5

Process finished with exit code 0
*/
public class ArrayDemo05 {
    public static void main(String[] args) {
        int[] arrays = {1,2,3,4,5};

        //JDK1.5  没有下标

        for (int array : arrays) {//For-Each循环
            System.out.println(array);
        }

        int[] a = reverse(arrays);

        printArray(a);

    }
  //数组作方法入参

    //打印数组元素
    public static void  printArray(int[] arrays){
        for (int i = 0; i < arrays.length; i++) {
            System.out.print(arrays[i] + " ");
        }
    }

    //反转数组
    public static int[] reverse(int[] arrays){
        int[] result = new int[arrays.length];

        //反转的操作
        for (int i = 0,j = result.length-1;i < arrays.length; i++,j--){
            result[j] = arrays[i];
        }

        return result;//数组作返回值
    }
}
/*
1
2
3
4
5
5 4 3 2 1 
Process finished with exit code 0
*/

二维数组

public class ArrayDemo06 {
    public static void main(String[] args) {

        int[][] array = {{1,2},{2,3},{3,4}};//[3][2]
        /*
        1,2  array[0]
        2,3  array[1]
        3,4  array[2]
         */
        System.out.println(array[0][0]);
            

    }
}
/*
1

Process finished with exit code 0
*/

冒泡排序

import java.util.Arrays;

public class ArrayDemo08 {
    public static void main(String[] args) {
        int[] a = {1, 54, 2, 2685, 3, 5, 18, 56, 4};

        int[] sort = sort(a);

        System.out.println(Arrays.toString(sort));
    }

    /*冒泡排序
      1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置
      2.每一次比较,都会产生出一个最大,或是最小的数字
      3.下一轮则可以少一次排序4
      4.依次循环,直到结束
    */

    public static int[] sort(int[] array) {
        //临时变量
        int temp = 0;

        //外层循环,判断我们要走多少次
        for (int i = 0; i < array.length - 1; i++) {
            //内层循环,比较两个数,如果第一个数,比第二个数大,则交换位置
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j + 1] < array[j]) {
                    temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }

        }

        return array;


    }
}
/*
[1, 2, 3, 4, 5, 18, 54, 56, 2685]

Process finished with exit code 0
*/

稀疏数组

public class ArrayDemo09 {
    public static void main(String[] args) {
        //1.创建一个二维数组 11*11 0:没有棋子  1:黑棋   2:白棋
        int[][] array1 = new int[11][11];
        array1[1][2] = 1;
        array1[2][3] = 2;
        //输出原始的数组
        System.out.println("输出原始的数组");

        for (int[] ints : array1){
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }

        System.out.println("===============================");

        //转换为稀疏数组保存
        //获取有效值的个数
        int sum = 0;
        for (int i = 0; i < 11; i++){
            for (int j = 0; j < 11; j++){
                if (array1[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("有效值的个数:" + sum);

        //2.创建一个稀疏数组
        int[][] array2 = new int[sum+1][3];

        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;

        //遍历二维数组,将非零的值,存放稀疏数组中
        int count = 0;
        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if (array1[i][j]!=0){
                    count++;
                    array2[count][0] = i;
                    array2[count][1] = j;
                    array2[count][2] = array1[i][j];
                }
            }
        }

        //输出稀疏数组
        System.out.println("稀疏数组");

        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i][0] + "\t"
                   + array2[i][1] + "\t"
                   + array2[i][2] + "\t");
        }

        System.out.println("==================================");
        System.out.println("还原");
        //1.读取稀疏数组
        int[][] array3 = new int[array2[0][0]][array2[0][1]];

        //2.给其中的元素还原它的值
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }

        //3.打印
        System.out.println("输出还原的数组");

        for (int[] ints : array3){
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }

    }
}
/*
输出原始的数组
0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	2	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
===============================
有效值的个数:2
稀疏数组
11	11	2	
1	2	1	
2	3	2	
==================================
还原
输出还原的数组
0	0	0	0	0	0	0	0	0	0	0	
0	0	1	0	0	0	0	0	0	0	0	
0	0	0	2	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	
0	0	0	0	0	0	0	0	0	0	0	

Process finished with exit code 0
*/