Java Foundation

Posted by stormx on Fri, 10 Sep 2021 03:22:19 +0200

array

1. Memory analysis:

Array memory analysis:

2. Basic characteristics of array

  • The length is determined. Once created, the size cannot be changed. If the boundary is exceeded, an error is reported: ArrayIndexOutofBoundsException
  • Elements must be of the same type
  • The elements in the array can be any data type, including basic and reference types
  • Array variables belong to the reference type, and arrays can also be regarded as objects. Each element in the array is equivalent to the member variables of the object
  • Data itself is also an object. The objects in Java are in the heap, so the array saves the original type or other object types. The array object itself is in the heap

3. Use of array

  • For each loop (strongly typed loop): no subscript, suitable for traversing output

            int[] a={1,2,3};
            for (int i : a) { //No subscript
                System.out.println(i);
            }
    
  • Array as method arguments

  • Array as return value

package com.bobo.base;

import java.util.Scanner;

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

    //Invert array
    public static int[] reverse(int[] arrays){
        int temp;
        for(int i=0;i<arrays.length/2;i++){
            temp=arrays[i];
            arrays[i]=arrays[arrays.length-1-i];
            arrays[arrays.length-1-i]=temp;
        }
        return arrays;
    }

    //Print array elements
    public static void printArray(int[] arrays){
        for(int i=0;i<arrays.length;i++){
            System.out.print(arrays[i]+" ");
        }
    }
}

4. Two dimensional array

  • Two dimensional array structure analysis: actually, it can be understood that a one-dimensional array is stored in the elements of each one-dimensional array

  • Definition of two-dimensional array

 int[][] array={{1,2},{2,3},{3,4},{4,5}}; //[4][2]
        System.out.println(array.length); //Number of rows of the output array
        System.out.println(array[0].length); //Number of columns in the output array
  • Three ways of traversing the output of two-dimensional array

    • Common cycle output

      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]+"\t");
                 }
                 System.out.println();
             }
      
    • Strong cyclic output

      int[][] array={{1,2},{2,3},{3,4},{4,5}};
      
             for(int[] i : array){
                 for(int n : i){
                     System.out.print(n+"\t");
                 }
                 System.out.println();
             }
      

5. Arrays class

Summary of common methods:

  • Assign a value to the array: through the fill method
  • Sort the array: through the sort method (ascending order)
  • Compare arrays: use the equals method to compare whether the element values in the array are equal
  • Find array elements: binary search can be performed on the sorted array through the binarySearch method
  • Output array: through toString() method (but will output brackets and commas)

6. Sparse array

  • When most elements in an array are 0 or an array with the same value, you can use a sparse array to save the array
  • How sparse arrays are handled
    • How many rows and columns are there in the record array? How many different values are there
    • Record elements, rows, columns and values with different values

  • example

    First traverse the original array, then convert it into a sparse array, and then get the original array according to the sparse array

    public class Demo {
        public static void main(String[] args) {
            int[][] array1=new int[11][11];
            array1[1][2]=1;
            array1[2][3]=1;
            System.out.println("Original array:");
            for(int[] ints : array1){
             for(int anInt :  ints){
                 System.out.print(anInt+"\t");
             }
                System.out.println();
            }
            System.out.println("=================================");
    
            //Convert sparse array
            int count=0;
            for(int i=0;i<11;i++){
                for(int j=0;j<11;j++){
                    if(array1[i][j]!=0)
                        count++;
                }
            }
            System.out.println("Number of significant digits:"+count);
    
            //Create a sparse array.
            int[][] array2=new int[count+1][3];
            array2[0][0]=11;
            array2[0][1]=11;
            array2[0][2]=count;
    
            //Traverse a two-dimensional array and store non-zero values in a sparse array
            int sum=0;
            for(int i=0;i<array1.length;i++){
                for(int j=0;j<array1[i].length;j++){
                    if(array1[i][j]!=0){
                        sum++;
                        array2[sum][0]=i;
                        array2[sum][1]=j;
                        array2[sum][2]=array1[i][j];
                    }
                }
            }
            System.out.println("Convert to sparse array:");
            //Output sparse array
            for(int[] i :array2){
                for(int a : i){
                    System.out.print(a+"\t");
                }
                System.out.println();
            }
            System.out.println("===================================");
    
            //Restore 2D array
            int[][] array3=new int[array2[0][0]][array2[0][1]];
            for(int i=1;i<=array2[0][2];i++){
                array3[array2[i][0]][array2[i][1]]=array2[i][2];
            }
            //Output 2D array
            for(int[] i:array3){
                for(int n : i)
                    System.out.print(n+"\t");
                System.out.println();
            }
        }
    }
    

    Operation results:

Topics: Java Interview