其他分享
首页 > 其他分享> > Day06__数组

Day06__数组

作者:互联网

数组

数组的定义

image-20220904153856555

数组的声明和创建

image-20220904154113064

image-20220904155217351

package array;

//数组的声明和创建
public class Demo01 {
    public static void main(String[] args) {
        int[] nums;//声明一个数组
        nums = new int[10];//创建一个数组
        //给数组分配值
        for (int i=0 ; i<nums.length ;i++ ){
            nums[i]=i*10;
        }
        System.out.println(nums[0]+" "+nums[9]);
    }
}

三种初始化及内存分析

image-20220904160133764

package array;

//三种初始化及内存分析
public class Demo02 {
    public static void main(String[] args) {
        //静态初始化
        int[] a={1,2,3,4,5};
        System.out.println(a[0]);

        //动态初始化:包含默认初始化
        int[] b = new int[5];
        b[0] = 0;
        System.out.println(b[0]+" "+b[4]);
    }
}

数组的四个基本特点

image-20220905101827656

image-20220905102154460

数组的使用

image-20220905102237855

package array;

//数组的使用
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.print(arrays[i]+" ");
        }
        System.out.println();

        //计算所有元素总和
        int sum=0;
        for (int i=0 ; i<arrays.length ; i++){
            sum+=arrays[i];
        }
        System.out.println("sum="+sum);

        //查找最大元素
        int max=arrays[0];
        for (int array : arrays) {
            if(array>max){
                max=array;
            }
        }
        System.out.println("max="+max);

        //打印反转元素
        int[] reverse = reverse(arrays);
        for (int i : reverse) {
            System.out.print(i+" ");
        }
        System.out.println();
    }

    //反转数组
    public static int[] reverse(int[] arrays){
        int[] result = new int[arrays.length];
        //反转
        for (int i=0 ; i< result.length ; i++){
            result[i] = arrays[arrays.length-1-i];
        }
        return result;
    }
}

多维数组

image-20220905103544640

package array;

//多维数组
public class Demo04 {
    public static void main(String[] args) {
        int[][] array = {{1,1},{2,2},{3,3}};
        for (int[] ints : array) {
            for (int anInt : ints) {
                System.out.println(anInt);
            }
        }
    }
}

Arrays讲解

image-20220905104845195

package array;

//Arrays讲解
import java.util.Arrays;
import java.util.Collections;

//Arrays类讲解
public class Demo05 {
    public static void main(String[] args) {
        int[] a = {1,2,15,773,534,35734,3,5,643};
        System.out.println(Arrays.toString(a));
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));


        Arrays.fill(a,0);//填充
        System.out.println(Arrays.toString(a));
    }
}

冒泡排序和选择排序

image-20220905110326883

package array;

import java.util.Arrays;

//冒泡排序和选择排序
public class Demo06 {
    public static void main(String[] args) {
        int[] a = {1,2,15,773,534,35734,3,5,643};
        int[] b = {1,2,15,773,534,35734,3,5,643};

        //冒泡排序
        int temp;
        //外层循环,控制比较的轮数
        for (int i = 0 ; i < a.length-1 ; i++) {
            for (int j = 0 ; j < a.length-1-i ; j++) {//每轮比较的次数,每经历一轮比较的个数少一个
                if(a[j] > a[j+1]){//比较相邻的两个数的大小
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(a));

        //选择排序
        for (int i = 0 ; i < b.length-1 ; i++) {
            for (int j = i+1 ; j < b.length ; j++) {
                if(b[j] < b[i]){
                    temp = b[j];
                    b[j] = b[i];
                    b[i] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(b));
    }
}

稀疏数组

image-20220905112501020

image-20220905121322836

package array;

import java.lang.reflect.Array;

//稀疏数组
public class Demo07 {
    public static void main(String[] args) {
        //创建一个数组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+" ");
            }
            System.out.println();
        }
        System.out.println("---------------------");

        //转化为稀疏数组保存
        //获取有效值的个数
        int sum=0;
        for (int[] ints : array1) {
            for (int anInt : ints) {
                if(anInt != 0){
                    sum++;
                }
            }
        }
        //创建一个稀疏数组
        int[][] array2 = new int[sum+1][3];
        array2[0][0] = 11;
        array2[0][1] = 11;
        array2[0][2] = sum;
        //遍历二维数组,将非0的值存放稀疏数组中
        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[] ints : array2) {
            for (int anInt : ints) {
                System.out.print(anInt + " ");
            }
            System.out.println();
        }
        System.out.println("---------------------");

        //还原稀疏数组
        int[][] array3 = new int[array2[0][0]][array2[0][1]];
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }
        //输出还原数组
        System.out.println("还原数组");
        for (int[] ints : array3) {
            for (int anInt : ints) {
                System.out.print(anInt+" ");
            }
            System.out.println();
        }
    }
}

标签:__,int,Day06,System,数组,println,public,out
来源: https://www.cnblogs.com/lyc2001/p/16657709.html