Today's tip is about java of Arrays class ( Tool class for array operation ) Learning of .
first , Let's first understand Arrays What is a class , about Arrays class :
Arrays Class in java.util In the package , It mainly includes various methods of manipulating arrays
Guide package when using :
import java.util.Arrays
secondly , Learn what methods this class has and how to use them :
Arrays Class contains various methods for manipulating arrays ( Such as sorting and searching ).
This class also contains a static factory , You can think of arrays as lists .“ Static factory ” explain Arrays Class contains many static methods , So that programmers can easily call .( Referring to the invocation of static methods , We can use it as an example method
object . Method name Make a call ( Not recommended ); Can also be used Class name . Method name Make a call ( recommendation ).)
Here are some Arrays Common methods of classes :
1.Arrays.toString( Array name a)
public static String toString( type [] a) // Remember to change the type to the corresponding data type when using
Method function : Returns a string representation of the contents of a specified array . The string representation consists of a list of array elements , Enclosed in square brackets ( "[]" ) in . Adjacent elements are composed of characters ", "
( Comma followed by a space ) Separate . Element is converted to a string by String.valueOf(short). return "null" If a yes null.
Why use this method ? Not directly :
System.out.println(a); //a Is an array name
First of all, this is definitely not possible , Because the output is an array a Address of ;
Of course, we can also use for Loop method outputs the elements in the array one by one , Although this is feasible , But if we just want to see what elements are in the array , There's no need to do this , use Arrays.toString( Array name )
You can directly output the contents of the array .
Test code :
import java.util.Arrays; public class Dem1 { public static void main(String[]
args) { int[] a= {1,4,3,2}; System.out.println(a);
System.out.println(Arrays.toString(a)); } }
Running results :
[Ljava.lang.Integer;@5ca881b5 [1, 4, 3, 2]
2.Arrays.sort( Group name a)
public static void sort( type [] a)
Default ascending sort of array .
Implementation considerations : The sorting algorithm is Vladimir Yaroslavskiy,Jon Bentley and Joshua Bloch Two axis quick sort provided .
The algorithm is available on many datasets O(n log(n)) performance , Cause other quicksort to degrade to secondary performance , And usually more than traditional ( Uniaxial )Quicksort Achieve faster .
For us, Xiaobai is undoubtedly using for Loop statements , It can be said that I have a headache , therefore Arrays Class sort() The method is undoubtedly beneficial to Xiaobai .
Here is the code test :
import java.util.Arrays; public class Dem1 { public static void main(String[]
args){ int[] a= {1,4,3,2}; System.out.println(Arrays.toString(a)); // Contents of array before sorting
Arrays.sort(a); System.out.println(Arrays.toString(a)); // Contents of sorted array } }
Running results :
[1, 4, 3, 2] // Before sorting [1, 2, 3, 4] // After sorting
3.Arrays.equals( Array name 1, Array name 2)
public static boolean equals( type [] a, type [] a2)
If two specified arrays are equal to each other , Then return true .
If both arrays contain the same number of elements , The two arrays are considered equal , And all corresponding element pairs in the two arrays are equal .
in other words , If two arrays contain the same elements in the same order , Then they are equal . in addition , If both array references are null, Then they are considered equal .
take care : The two arrays being compared must be of the same type !!!
Test code :
import java.util.Arrays; public class Dem1 { public static void main(String[]
args){ int[] arr1= {1,4,3,2}; // Original array int[] arr2= {1,6,5,7}; // Same number of array elements , Different elements
int[] arr3= {1,4,3,2,5}; // The front elements of the array are the same , Different number int[] arr4= {1,4,3,2}; // Array elements and number are the same
double[] dou= {1,4,3,2}; // Array elements and number are the same , But the array type is different
System.out.println(Arrays.equals(arr1, arr2));
System.out.println(Arrays.equals(arr1, arr3));
System.out.println(Arrays.equals(arr1, arr4)); //
System.out.println(Arrays.equals(arr1, dou)); Error reporting } }
Running results :
false // Same number of array elements , Different elements false // The front elements of the array are the same , Different number true // Array elements and number are the same
Error reporting information :
Exception in thread "main" java.lang.Error: Unresolved compilation issues : type Arrays Methods in
equals(int[], int[]) For parameters (int[], double[]) Not applicable Syntax error , take “VariableDeclarators” Insert to full
LocalVariableDeclaration in Syntax error , take “;” Insert to full LocalVariableDeclarationStatement in
Cannot resolve Error reporting
That's all for today's knowledge sharing , Thank you for reading !( Xiaobai records learning , Please give me more advice ; The method and introduction of this article are based on jdk1.8
API Help documents find , If something is wrong, it may be a translation problem , Or maybe it was the wrong hand ( dog's head ). Then help the document , You can search online , Many resources , If you can't find it, you can confide in me .)
Technology
Daily Recommendation