Java array sorting I [cloud map smart link]

Keywords: Java less

;

Array concept:
Array is a container with fixed data length to ensure the consistency of data types of multiple data.

To declare an array is to draw a series of continuous spaces in the memory space

Array name represents the first address of continuous space

All elements of the array can be accessed successively through the first address

 

1, Three ways to define arrays

  • Mode 1
`Data type [] array name = new data type [length]`

Detailed explanation of array definition format

Data type: the created array container can store any data type.  

[]: represents an array.

Array name: set a variable name for the defined array to meet the identifier specification. You can use the name to operate the array.

[length]: the length of an array, indicating how many elements can be stored in the array container

Note: array has fixed length property. Once the length is specified, it cannot be changed.

 

  • Mode 2: use static initialization to define array (complete format)
Data type [] array name = new data type [] {element 1, element 2, element 3...};

In development, full syntax mode is strongly recommended for static array initialization

 

  • Mode 3: use static initialization to define array (simplified format)
Data type [] array name = {element 1, element 2, element 3...}

Array's biggest flaw: fixed length.

 

2, Access to arrays

1. The elements in the array are accessed through the index of the array. Format: array name [index]

2. Get the length property of array format: array name. Length

The maximum index value of an array is the array name. length-1

3. Traversal of arrays: using for loops

 

3, static keyword

1. Function

static keyword to modify variables, methods, and code blocks.

In the process of using, the main purpose is to call methods without creating objects. The following tool classes can reflect the convenience of static methods.

 

2. Use of static keyword

Class variable

   

*When static modifies a member variable, it is called a class variable
 *Each object of this class shares the value of the same class variable. Any object can change the value of the class variable, but it can also operate on the class variable without creating the class object.

 

Static method

*When static modifies a member method, it is called a class method. Static methods have static in the declaration. It is recommended to use the class name to call

*Considerations for static method calls: (static methods can only access static members)

Static methods can directly access class variables and static methods.
Static methods cannot directly access ordinary member variables or member methods. On the contrary, member methods can directly access class variables or static methods.
this keyword cannot be used in static methods.

 

Static code block

Definition location: outside method in class

Execute: execute once as the class is loaded, prior to the execution of main method and construction method

Function: initializes and assigns values to class variables

 

IV. Java Arrays tool class

The Arrays class is a utility class that contains many methods for manipulating Arrays. All methods in this Arrays class are static modified

1,type[]   copyOf(type[] original, int length)

  • This method copies the original array into a new array, where length is the length of the new array.
  • If the length is less than the length of the original array, the new array is the first length element of the original array
  • If the length is greater than the length of the original array, the leading element of the new array is all elements of the original array, followed by 0 (numeric type), false (boolean type) or null (reference type).

       int[] arr1 = {1, 2, 3, 4, 5};
       int[] arr2 = Arrays.copyOf(arr1,4);
    
       int[] arr3 = Arrays.copyOf(arr1, 10);
    
       System.out.println(Arrays.toString(arr2)); // [1, 2, 3, 4]
       System.out.println(Arrays.toString(arr3)); // [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]

 
2. public static String toString(int[] a): returns a string representation of the contents of a specified array.

public static void main(String[] args) {
    int[] arr = {2,34,35,4,657,8,69,9};
    // Print array, output address value
    System.out.println(arr);  // [I@1b6d3586
 
    // Array content to string
    String s = Arrays.toString(arr);
    System.out.println(s);  // [2, 34, 35, 4, 657, 8, 69, 9]
}

3. public static void sort(int[] a): sort the specified array of type int in numerical ascending order


public static void main(String[] args) {
// Define int array
int[] arr = {24, 7, 5, 48, 4, 46, 35, 11, 6, 2};
System.out.println("Before sorting:"+ Arrays.toString(arr)); 
// Before sorting: [24, 7, 5, 48, 4, 46, 35, 11, 6, 2]
 
// Ascending sort
Arrays.sort(arr);
System.out.println("After sorting:"+ Arrays.toString(arr));
// After sorting: [2, 4, 5, 6, 7, 11, 24, 35, 46, 48]
}

**4,Arrays.sort(Object[] array, int from, int to)
Sort the specified range of array elements (the sorting range is from elements with subscript from to-1)**

 

5, Arrays.fill(Object[] array,Object object) can fill the same value for array elements

    int[] nums = {2,5,0,4,1,-10};

    Arrays.fill(nums, 1);

    for(int i :nums){

        System.out.print(i+" ");
    }


    /* Before: 2 5 0 4 1 - 10
     * Results: 11 11 11 1 
     */

6, Arrays.asList(T... a) array conversion (common)

int array = new int[]{3, 10, 4, 0, 2};
List<int[]> ints = Arrays.asList(array);
 
Integer arr[] = new Integer[]{3, 10, 4, 0, 2};
List<Integer> integers = Arrays.asList(arr);

The difference between int type and wrapper type array Collections:

The array of the original data type int calls asList to get a List with only one element, which is an array of element type.

The encapsulation class Integer array call asList is to add each element of the array to the List.

 

7. Arrays. equals(int [] a, int [] a2) array comparison two array elements are equal if they contain the same elements in the same order

public static void main(String[] args) {
    int[] arr = {2,34,35,4,657,8,69,9};
    int[] arr2 = {2,34,35,4,657,8,69,9};
    System.out.println(Arrays.equals(arr,arr2)); // true
 
    Integer[] a =  new Integer[]{2,34,35};
    Integer[] a1 =  new Integer[] {2,34,35};
    System.out.println(Arrays.equals(a,a1)); // true
}

8,parallelPrefix (T[] array, BinaryOperator<T> op)

Parallel prefix (t [] array, int fromindex, int toindex, binaryoperator < T > OP) binary iteration, binary operation on the original array content

public static void main(String[] args) {
    Integer[] arrayTest={1,2,3,4,5,7,6};
    //Binary iteration, binary operation of the original array content
    Arrays.parallelPrefix(arrayTest,(x,y)->x*y);
    System.out.println(Arrays.toString(arrayTest)); // [1, 2, 6, 24, 120, 840, 5040]
 
    //Within the specified subscript range, binary operation is performed on the original array content, and the subscript contains the header and does not contain the tail
    Integer[] arrayTest2={1,2,3,4,5,7,6};
    Arrays.parallelPrefix(arrayTest2,0,3,(x,y)->x*y);
    System.out.println(Arrays.toString(arrayTest2));  // [1, 2, 6, 4, 5, 7, 6]
}

 

9. Parallel sort: to sort the array in ascending or custom order

public static void main(String[] args) {
    Integer[] arrayTest={1,2,3,4,5,6,7};
    //The subscript + 2 of each element is then assigned to the corresponding element of the array
    Arrays.parallelSetAll(arrayTest,(x)->(x+2)); //[2, 3, 4, 5, 6, 7, 8]
    System.out.println(Arrays.toString(arrayTest));
 
    Integer[] arrayTest2={3,1,6,4,10,7,5};
   // Sort the elements within the specified subscript range by the specified sorting method, including the header and excluding the tail
    Arrays.parallelSort(arrayTest2,0,5,(x,y)->y.compareTo(x));
    System.out.println(Arrays.toString(arrayTest2)); // [10, 6, 4, 3, 1, 7, 5]

10 , Arrays.setAll(int [] array,IntUnaryOperator generator) serial to set array elements

Integer[] arrayTest={1,2,3,4,5,6,7};
//The subscript + 3 of each element is then assigned to the corresponding element of the array
Arrays.setAll(arrayTest, (x)->(x+3));
System.out.println(Arrays.toString(arrayTest)); // [3, 4, 5, 6, 7, 8, 9]

11, Arrays.stream (int [] array) construct flow

There will be bubble sorting later, which will be shared later

Posted by heals1ic on Thu, 18 Jun 2020 03:16:17 -0700