Learn JAVA array again

initialization

There are generally two methods for array. One is to allocate memory space first and then initialize data, and the other is to specify data directly when initializing the array.

int[] array1 = new int[10];
array[1] = 10;

int[] array2 = new int[]{10,11};

In addition, there is another way to create and initialize arrays through reflection, which can dynamically create different types of arrays.

int[] intArr = (int[]) Array.newInstance(int.class, 10);
Array.set(intArr, 0, 10);
System.out.println(Array.get(intArr, 0));

characteristic

The array itself is a reference type, and the length of the array cannot be modified after creation. The contents of the array can be accessed through the index. The index range is 0 - (length-1). In memory, the array corresponds to a continuous memory space, which is also the premise that the array can be accessed through the index, because the memory space corresponding to the index can be located through the first address + offset.

ergodic

There are two main ways to traverse arrays. One is for+index, and the other is to traverse by enhancing the for loop.

//Array initialization
int[] arr = new int[10];
Arrays.parallelSetAll(arr, i -> i);

//Traversal by index
for (int i = 0;i < arr.length;i ++) {
   System.out.println(arr[i]);
}

//Traversal by enhancing the for loop
for (int value : arr) {
   System.out.println(value);
}

Arrays tool class

Arrays is a tool class used to operate arrays under the java.util package. It mainly includes the following types of operations

  • sort/paralleSort
  • binarySearch

  • parallePrefix

  • fill

  • copyOf/copyOfRange

  • asList

  • equals/hashCode/toString

  • spliterator

Sort / parallelsort can sort basic data types and reference data types. Certain requirements must be met when sorting reference data types: the Comparable interface is implemented or the Comparator comparator is specified. For example, the following is the annotation of one of the sort methods:

Sorts the specified array of objects into ascending order, 
according to the natural ordering of its elements. 
All elements in the array must implement the Comparable interface

Parallelsort is a sort that can support parallel computing. When the array length reaches 1 < < 13 (8192), the parallel mechanism will be triggered. Specifically, the merge sort algorithm is adopted, and the parallel task is driven by the ForkJoin thread pool. Internal notes are given below:

The sorting algorithm is a parallel sort-merge that breaks the array into sub-
arrays that are themselves sorted and then merged. When the sub-array length 
reaches a minimum granularity, the sub-array is sorted using the appropriate 
Arrays.sort method. If the length of the specified array is less than the minimum 
granularity, then it is sorted using the appropriate Arrays.sort method. The 
algorithm requires a working space no greater than the size of the specified range 
of the original array. The ForkJoin common pool is used to execute any parallel tasks

The binraySearch method is simply understood to find the target value from the target array through binary search, but it provides many overloaded methods, such as methods supporting different data types and searching in the specified interval of the array. When searching the data in the array of application types, the elements in the array must implement the Comparable interface.

Parallelprefix is a method introduced in jdk1.8. Let's take a look at the method signature

public static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) ;

See the following examples for specific functions:

//After initialization, the original array is [0,1,2]
int[] arr = new int[3];
Arrays.parallelSetAll(arr, i -> i);

Arrays.parallelPrefix(arr, new IntBinaryOperator() {
     //left represents the previous value and right represents the current value
     @Override
     public int applyAsInt(int left, int right) {
                //The logic here is to add the previous value to the current value
                return left + right;
    }
});

for (int i = 0;i < arr.length;i ++) {
    System.out.println(arr[i]);
}

After the above operation, the final array result is: 0, 1, 3

The fill method is to fill the contents of the array. It supports different types of arrays and filling data in the specified interval of the array.

public static void fill(short[] a, short val);
public static void fill(short[] a, int fromIndex, int toIndex, short val);
...

copyOf/copyOfRange provides the function of copying the data in the array. copyOf copies the full amount. copyOfRange can copy the data in the specified interval in the original array

public static long[] copyOf(long[] original, int newLength);
public static <T> T[] copyOfRange(T[] original, int from, int to);

asList is my favorite method. It can convert an array into a Collection, and then use the Stream API of the Collection to easily perform various operations on the data in the heap array, but the disadvantage is that it may generate more temporary objects and consume memory space.

public static <T> List<T> asList(T... a);

equals/hashCode/toString are the basic operations for arrays. equals compares whether two arrays are equal. hashCode adds each element in the array to the algorithm for calculating hashCode, and finally obtains a more reasonable hashCode value. ToString formats the string output of an array. If you directly call the toString method of an array, what is printed is actually the memory address of the array object, which is not readable.

Tags: Java Back-end

Posted on Sat, 27 Nov 2021 21:39:55 -0500 by mark123$