Java from entry to the master Chapter 6 array

table of Contents

Creating and using arrays

The basic operation of the array

Array sorting algorithm


Creating and using arrays

  • Create a one-dimensional array
    • int arr []; arr = new int [10]; first statement, and then allocating new memory
    • int [] arr = new int [  10]; directly allocate memory declaring
    • int arr []; arr = new int [] {1, 2, 3}; Initialization
    • int[] arr = {1, 2, 3};
  • Create a two-dimensional array
    • int arr [] []; arr = new int [2] [3]; first statement, it is allocated
    • int [] [] arr = new int [2] [3]; allocate memory directly declaring
    • int [] [] arr = new int [2] []; arr [0] = new int [2]; arr [1] = new int [3]; allocate memory separately for each dimension
    • int [] [] arr = {{1, 2}, {3, 4}}; Initialization

The basic operation of the array

  • Gets an array of length

    • arr.length; array property length
  • Replace fill array elements
    • Arrays.fill (int [] a, int value); fill all the array values ​​with the values
    • Arrays.fill (int [] a, int fromIndex, int toIndex, int value); filled specified index value range, including fromIndex, not including toIndex
      • Index out of range will be reported abnormal
  • Sequence
    • Arrays.sort (arr); Ascending
  • Copy the array
    • Arrays.copyOf (arr, int newlength); copy the array, specifies the length of the new array
    • Arrays.copyOfRange (arr, int fromIndex, int toIndex); copies the specified range of the array into a new array, toIndex may be greater than the length of arr
    • arr.clone (); clone array
  • An array of query
    • Arrays.binarySearch (Object, Object key); lookup key position in the array, return the index is present, there is no return insertion point, returns the first key element is greater than the negative indices {4, 25, 10}, if lookup 8 negative index 10 is returned, {4, 8, 10, 10, 25} the index is 2, returns -2
    • Arrays.binarySearch(Object, int fromIndex, int toIndex, Object key);
      • Always returns a negative index of the insertion point when the return value is not found, to ensure that the return value is found greater than or equal to 0
      • Index out of range will be reported abnormal

Array sorting algorithm

  • Bubble Sort
    • Comparison between adjacent elements, and swap.
    • Len is the length of the array, the number of outer loop control wheel from len 1-times, the inner loop controls each one index of the last element from the one to the new one. 1 increases the number of rounds, at least one inner loop the largest index.
  • Direct Selection Sort
    • Direct comparison, no exchange, only the index record, and then exchanged each time to find the maximum value.
    • Cycle similar bubble sort, but more updated minimum or maximum value where the index, the minimum or maximum exchange value of the last index of the new round of direct after the end of the inner loop, the last index for the minimum or maximum value.
  • Reverse the sort
package ex6_array;

public class ex6_20_Sort {
    int cmpNum = 0;
    int chgNum = 0;

    public static void main(String[] args) {
        int[] arrOri = {63, 4, 24, 1, 3, 15};
        int[] arr = arrOri.clone();
        int[] arr2 = arrOri.clone();
        int[] arr3 = arrOri.clone();
        ex6_20_Sort sorter = new ex6_20_Sort();  //创建排序类的对象
        sorter.bubbleSort(arr);  //调用冒泡排序方法将数组排序
        sorter.selectSort(arr2);  //调用直接选择排序方法将数组排序
        sorter.reverseSort(arr3);  //反转排序

    }

    /**
     * 冒泡排序
     *
     * @param array 要排序的数组
     */
    public void bubbleSort(int[] array) {
        cmpNum = 0;
        chgNum = 0;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 1; j < array.length - i; j++) {
                if (array[j - 1] > array[j]) {
                    int tmp = array[j - 1];
                    array[j - 1] = array[j];
                    array[j] = tmp;
                    chgNum++;
                }
                cmpNum++;
            }
        }
        showArray(array);
        System.out.println("bubbleSort--" + "cmpNum: " + cmpNum + " chgNum: " + chgNum);
    }

    /**
     * 直接选择排序
     *
     * @param array 要排序的数组
     */

    public void selectSort(int[] array) {
        cmpNum = 0;
        chgNum = 0;
        int index;
        for (int i = 0; i < array.length - 1; i++) {
            index = 0;
            for (int j = 1; j < array.length - i; j++) {
                if (array[j] > array[index]) {
                    index = j;
                    chgNum++;
                }
                int tmp = array[array.length - i - 1];
                array[array.length - i - 1] = array[index];
                array[index] = tmp;
                cmpNum++;
            }
        }
        showArray(array);
        System.out.println("selectSort--" + "cmpNum: " + cmpNum + " chgNum: " + chgNum);
    }

    /**
     * 反转排序
     *
     * @param array 要排序的数组
     */
    public void reverseSort(int[] array) {
        System.out.print("before reverse: ");
        showArray(array);
        int tmp;
        int len = array.length;
        for (int i = 0; i < len / 2; i++) {
            tmp = array[i];
            array[i] = array[len - i - 1];
            array[len - i - 1] = tmp;
        }
        System.out.print("after reverse:");
        showArray(array);
    }

    /**
     * 显示数组中的所有元素
     *
     * @param array 要显示的数组
     */
    public void showArray(int[] array) {
        for (int x : array) {
            System.out.print(x + "-");
        }
        System.out.println();
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

Published 46 original articles · won praise 0 · Views 1033

Guess you like

Origin blog.csdn.net/weixin_37680513/article/details/103324555