反转数组、遍历打印二维数组、冒泡排序、稀疏数组

反转数组

public class ArrayDemo01 {
    public static void main(String[] args) {
        int[] arrays={1,2,3,4,5};
        int[] reverse=reverse(arrays);
        printArray(reverse);
    }

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

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

        return result;
    }

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

输出结果为:

5 4 3 2 1 

遍历打印二维数组

public class ArrayDemo02 {
    public static void main(String[] args) {
        int[][] array={{1,2},{2,3},{3,4},{4,5}};//定义二维数组

        //遍历打印二维数组
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }

}

输出结果为:

1 2 
2 3 
3 4 
4 5 

冒泡排序

import java.util.Arrays;

public class ArrayDemo03 {

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

    public static void main(String[] args) {
        int[] array={1,4,65,23,98,55,13,76,3};
        System.out.println(Arrays.toString(sort(array)));
    }

    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, 3, 4, 13, 23, 55, 65, 76, 98]

稀疏数组

public class ArrayDemo04 {
    public static void main(String[] args) {
        //1.创建一个二维数组10*10
        int[][] array1=new int[10][10];
        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 < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (array1[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("有效值的个数为:"+sum);


        //创建一个稀疏数组
        int[][] array2=new int[sum+1][3];
        array2[0][0]=10;
        array2[0][1]=10;
        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.print(array2[i][0]+"\t"
                    +array2[i][1]+"\t"
                    +array2[i][2]+"\t");
            System.out.println();
        }

        //还原为二维数组
        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	1	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	
===================
有效值的个数为:2
输出稀疏数组:
10	10	2	
1	2	1	
2	3	2	
=====================
还原为二维数组:
输出还原的数组:
0	0	0	0	0	0	0	0	0	0	
0	0	1	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	
上一篇:数组


下一篇:稀疏数组