java5:数组

数组

1. 数组概述

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

2. 数组声明创建

  • 首先必须声明数组变量,才能再程序中使用数组

    public class Array_test {
        public static void main(String[] args) {
            /*
            两种声明方式:
            1. 数据类型声明数值(java常用方式)
            2. 在变量中声明数组
             */
              int[] array1;
              array1 = new int[10]; //创建int类型可以储放10个数的array1数组
              int array2[];
              array2 = new int[5]; //创建int类型可以储放10个数的array2数组
        }
    }
    
  • 数组赋值

public class Array_test {
    public static void main(String[] args) {
        int[] array1;
        array1 = new int[10];
        // 给数组赋值
        array1[0] = 1;
        array1[1] = 2;
        System.out.println(array1[0]);
        System.out.println(array1[1]);
        System.out.println(array1[9]);
    }
}
1
2
0
  • 使用length方法查询数组长度
    • 遍历数组并赋值
public class Array_test {
    public static void main(String[] args) {
        //int[] array1;
        //array1 = new int[10];
        //以上两句代码可简写为
        int[] array1 = new int[10];
        // 给数组赋值
        int sum = 0;
        for (int i = 0; i < array1.length; i++) {
            array1[i] = i;
            sum += array1[i];
        }
        System.out.println(sum);
    }
}
45

3. 数组使用

  • 增强型for循环(for each),省略了下标
public class Array_test {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        for (int array :
                array1) {
            System.out.println(array);
        }
    }
}
1
2
3
4
5
  • 通过函数调用输出数组
public class Array_test {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        printArray(array1);
    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
}
1 2 3 4 5 
  • 数组数值反转
public class Array_test {
    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        printArray(reverse(array1));
    }
	//排序函数
    public static int[] reverse(int[] array) {
        int[] result = new int[array.length];
        for (int i = 0, j = result.length - 1; i < array.length; i++, j--) {
            result[j] = array[i];
        }
        return result;
    }
    //打印函数
    public static void printArray(int[] array){
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }
}
5 4 3 2 1 

4. 多维数组

public class Array_test {
    public static void main(String[] args) {
        int[][] array = {{1, 2}, {3, 4}, {5, 6}};
        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

5. Arrays类

  • 冒泡排序
import java.util.Arrays;

public class Array_test {
    public static void main(String[] args) {
        int[] a = {0, 212, 245, 43, 1, 34, 12};
		//Arrays.sort(a);
		//System.out.println(Arrays.toString(minToMax(a)));
        showString(minToMax(a));
    }

    public static void showString(int[] a) {
        for (int i = 0; i < a.length; i++) {
            if (i == 0) {
                System.out.print("[");
            }
            if (i == a.length - 1) {
                System.out.print(a[i] + "]");
            } else {
                System.out.print(a[i] + ",");
            }
        }
    }

    public static int[] minToMax(int[] a) {
        int max = 0;
        for (int i = 0; i < a.length - 1; i++) {
            boolean flag = true;
            for (int j = 0; j < a.length - 1 - i; j++) {
                if (a[j] > a[j + 1]) {
                    max = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = max;
                    flag = false;
                }
            }
            if (flag != false) {
                break;
            }
        }
        return a;
    }
}

6. 稀疏数组

  • 当一个数组中大部分元素为0,或者为同一值的数组时,可以使用稀疏数组来保存该数组
  • 稀疏数组的处理方式是:
    • 记录数组一共有几行几列,有多少个不通值
    • 把具有不同值的元素和行列及值记录再一个小规模的数组中,从而缩小程序的规模
  • 在这里插入图片描述
public class ArrayTest2 {
    public static void main(String[] args) {
        int[][] array1 = new int[11][11];
        array1[1][2] = 1;
        array1[2][3] = 2;
        System.out.println("原始数组:");

        // 相当于python:for i in array1:
        for (int[] i : array1) { // 输出每一行数组
            for (int j : i) { // 遍历每行数组的值
                System.out.print(j + "\t");
            }
            System.out.println();

        }
        System.out.println("=======================================");
        // 转换为稀疏数组
        // 1.获取有多少个有效值
        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];
        // 记录有效值:11行,11列,sum个有效值
        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];
        }

        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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值