arrrays class, bubble sort, sparse array

Arrays class

Open
the code : Output a set of arrays
1, _//print array elements Arrays.toString
_System.out .println (Arrays.toString ( a));

package com.array;

import java.util.Arrays;

public class ArrayDoment03 {
    public static void main(String[] args) {
        int[] a = {1,23,54,78,98,986756};

        //打印数组元素Arrays.toString
        System.out.println(Arrays.toString(a));//打印输出方法一
        printArrays(a);
    }
    public static void printArrays(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]+","+" ");
            }
        }

    }
}

Output result:

[1, 23, 54, 78, 98, 986756]
[1, 23, 54, 78, 98, 986756]

2. Sort the array elements (sort the array through the sort method) Arrays.sort (a); System.out.println ( Arrays.toString
( a ) ) ; Code sample: sort a group of arrays

package com.array;

import java.util.Arrays;

public class ArrayDoment03 {
    public static void main(String[] args) {
        int[] a = {445,23,89,78,98,986756};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));

    }
   }

output result;

[23, 78, 89, 98, 445, 986756]

3. Fill elements (assignment to the array through the fill method) Arrays.fill
( a,2,4,0); _//The array between 2 and 4 is filled with 0 _System.out.println ( Arrays.toString ( a) ); Code sample: The array between 2 and 4 is filled with 0

package com.array;

import java.util.Arrays;

public class ArrayDoment03 {
    public static void main(String[] args) {
        int[] a = {445,23,89,78,98,986756};
         Arrays.fill(a,fromindex:2,toindex:4,val:0);//2到4之间的数组填充为0
        System.out.println(Arrays.toString(a));

    } 
   }

Output result:

[445, 23, 0, 0, 98, 986756]

4. Compare elements (comparing arrays compares whether the elements in the array are equal through the equals method) Arrays.equals
( a,a1); System.out .println ("a
a1:"+(aa1));
System.out.println (“Arrays.equals(a,a1):”+Arrays.equals ( a ,a1)); Code sample: compare array a and array a1

package com.array;

import java.util.Arrays;

public class ArrayDoment03 {
    public static void main(String[] args) {
        int[] a = {445,23,89,78,98,986756};
        int[] a1 ={445,23,89,78,98,986756};
        Arrays.equals(a,a1);
        System.out.println("a==a1:"+(a==a1));
        System.out.println("Arrays.equals(a,a1):"+Arrays.equals(a,a1));
    }
    
  }

Output result:

a==a1:false
Arrays.equals(a,a1):true

5. Search subscripts in binary arrays: (binarysearch method to search array subscripts)
int[] sort = new int[]{445,23,89,78,98,986756};
int index = Arrays.binarySearch ( sort,89) ;
System.out .println ("The subscript of 89 is: "+index);
Code sample: output the subscript of 89

package com.array;

import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayDoment03 {
    public static void main(String[] args) {
        int[] sort = new int[]{445,23,89,78,98,986756};
        int index = Arrays.binarySearch(sort,89);
        System.out.println("89的下标是:"+index);
 }
}

Output result:

Bubble Sort

Code sample:

package com.array;

import java.lang.reflect.Array;
import java.util.Arrays;

public class ArrayDomen07 {
    public static void main(String[] args) {
        //冒泡排序;要烂熟于心
        int[] a ={23,34,45,67,2,12,5,87,98,2345};//程序执行第一步:将实参传入给形参

        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++){
            //内循环比较数组元素中两个数的大小,然后依次循环输出
            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;//程序执行第三步:得出结果后将数据返回给数组sort
    }
}

Output result:

[2345, 98, 87, 67, 45, 34, 23, 12, 5, 2]

sparse array

What is a sparse array;
when most of the elements in an array are 0, or are arrays with the same value, you can use a sparse array to save the array.
The processing method of sparse array is:
1. Record how many rows and columns the array has, and how many different values ​​there are
2. Record the elements, rows, columns and values ​​with different values ​​in a small-scale array, thereby reducing the scale of the program

Code: before compression

package com.array;

public class ArrayDomeno4 {
    public static void main(String[] args) {
        int[][]  a = new int[11][11];
        a[1][2]=1;
        a[2][3]=2;

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

result:

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	

After converting to a sparse array:

package com.array;

public class ArrayDomeno4 {
    public static void main(String[] args) {
        int[][] a = new int[11][11];
        a[1][2] = 1;
        a[2][3] = 2;
        
        System.out.println("输出原始数组:");
        for (int[] ints : a) {
            for (int anInt : ints) {
                System.out.print(anInt + "\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 (a[i][j]!=0){
                    sum++;
                }
            }
        }
        System.out.println("有效值为:"+sum);
        //2、创建一个稀疏数组的数组
        int[][] a2 = new int[sum+1][3];
        a2[0][0] = 11;
        a2[0][1] = 11;
        a2[0][2] = sum;

        //3、遍历二维数组,将非零的值,放到稀疏数组中
        int count = 0;
        for (int i = 0; i < a.length; i++){
            for (int j = 0; j < a[i].length; j++){
                if(a[i][j]!=0) {
                    count++;
                    a2[count][0] = i;//从第几行第一列存放他的第一个数值
                    a2[count][1] = j;
                    a2[count][2] = a[i][j];
                }
            }
        }
        //输出稀疏数组;
        System.out.println("稀疏数组");

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

        }
    }
}

Output result:

输出原始数组:
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	

Guess you like

Origin blog.csdn.net/m0_65792710/article/details/125582839