javaSE 数组

fill方法:给数组赋值

sort方法:对数组进行升序排序

equals方法:比较数组中的元素值是否相等

binarySearch方法:对排序好的数组进行二分查找

==========================================================================================

                                                                                                冒泡排序

public class ArrayDemo{

       public static void main(String[] args){

       int[] a = {1,2,3,5,6,7,15,35,5,5,554,0,,15,154,51,2,151}

       int[] sore = sore(a);

       System.out.prinln(Arrays.toString(sort));

}

public static int[] sort(int[] array){

      int   max = 0;

for(int i = 0; i < array.length-1; i++){

       for(j = 0; j < array.length - 1 - i; j++){   //相邻俩个数

       if(array[j] < array[j+1]){

       max =  array[j+1];

       array[j+1] = array[j]

       array[j] = max;

}       

}

}

}

package Anarrayof;
import java.util.Arrays;
public class ArrayDemo06 {
    public static void main(String[] args) {
        int[] a = {1,4,5,6,72,2,2,2,25,6,7};
        int[] sort = sort(a);
        System.out.println(Arrays.toString(sort));
    }
    //冒泡排序
    public static int[] sort(int[] array){
        int temp = 0;
        for (int i = 0; i < array.length-1; i++) {
            boolean flag = false;
            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;
                    flag = true;
                }
            }
            if (flag == false){
                break;
            }
        }
        return array;
    }
}

===========================================================================================

                                                                                               稀疏算法

package Anarrayof;

/**
 * @Description
 * @Author luowuji
 * @Date 2021/9/18 23:36
 */
public class ArrayDemo07 {
    public static void main(String[] args) {
        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);

        //创建一个稀疏数组

        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("还原");
        //读取稀疏数组
        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 + "\t");
            }
            System.out.println();
        }
    }
}

javaSE 数组

上一篇:常用Dos命令


下一篇:3ds Max 2016主工具栏不见了,如何恢复?