First of all, an idea editor, the editor's cursor changes to a black cursor can not move, click the Insert key .
an array is a linear data storage structure that is used to sacrifice auto-scaling to gain the only advantage over the collection-
the increase in query efficiency . itself is a reference type of data, so we want to use an array to first declare, initialize him, and its initialization of the completion also means that the size of this array will no longer change, and have the initial value. that is
, the definition array must be given the size of the array . The length of the array in Java does not change!
The stored data type is unique the reference to the array is stored inside the stack, and the actual object is stored in the heap memory. common methods can be as follows:
1. Definition and declaration of an array (must be given an array size!!!!! )
/*1. Define the array, must specify the size of the array, advantages: High query efficiency*/ //Arrays.fill (array name, value) is primarily assigned to the same value for each element in the array,//This method can be used to replace any type of array element by various overloaded forms.string[] S1 =NewString[4]; Arrays.fill (S1,"Wyy"); for(String s:s1) {System.out.println (s); }
String s2[]= {"Wyy", "Wzz", "WXX"}; System.out.println (arrays.tostring (S2));//Note: Direct output s2 can only get the address reference of the arraystring[] S3=Newstring[]{"Zyy", "zxx", "ZWW"}; System.out.println (Arrays.tostring (S3)); //Convert A string array to a string int[] S5 =New int[]{7, 3, 2, 8, 9};
2. Converting an array to a collection (set,list)
(1) Convert to List
/* 2. Convert the array to ArrayList Arrays.aslist (array) */ Listnew arraylist<>( Arrays.aslist (S2)); List.add ("whh"); System.out.println ("ArrayList contains:" + list.contains ("whh"));
(2) Convert to set
New Hashset<>(arrays.aslist (S2)); System.out.println ("Set Set contains:" + set.contains ("Wyy"));
(3) integral type array conversion
It is important to note that converting an array into a collection must be in the original type. Int[] cannot be converted directly to a collection, because the argument to the Aslist () method must be an object. Int[] should first be converted to integer[]. The same is true for arrays of other primitive types, which must first be converted to the corresponding array of wrapper class types.
Java is an object-oriented programming language, and it is said that everything is an object. But those eight data types, such as int double, are not objects, which contradicts the idea that everything in Java is an object, so in order to solve this problem, Java has a wrapper class that wraps the basic data types Let it become a class, and so on when the use of direct call on it can be. because the type of the collection must be an object, the add element type must be the original class .
Public Interface extends
New Integer[s5.length]; for (int i = 0; i < integerarray.length; i++) { = s5[i]; } SetNew hashset<>(arrays.aslist (Integerarray)); System.out.println (Set1); // [2, 3, 7, 8, 9]
3. Whether an array contains an element Arrays.tostring (S2). Contains converted to a string, and then using the Contain method
/ *3. If the array contains an element, use arrays.tostring (S2), and then use the string directly. Contain method */ System.out.println ("string contains:" + arrays.tostring (S2). Contains ("Wyx"));
4. Convert int value to byte array
byte[] bytes = Bytebuffer.allocate (4). Putint (). Array (); for (byte t:bytes) { System.out.format ("0x%x", t); // 0x0 0x0 0x0 0x5a }
5. Extension: How to see if an array contains an element
Method 1:arrays.tostring (S2). Contains converted to a string, and then using the contain method
Method 2: First use 2, convert to a collection, and then use the contain method
Method 3: Self-looping
Public Static Boolean Loop (string[] arr, string targetvalue) { for(string s:arr) { if( S.equals (targetvalue)) returntrue; return false ; }
The method 4:arrays.binarysearch (), which can be used only in ordered arrays, is recommended when the array stores data a lot.
You can calculate the time complexity by calculating long time=system.nanotime ().
Public Static Boolean BinarySearch (string[] arr, String targetvalue) { int j = Arrays.binarysearch (arr, Targetvalue); if (J > 0) { returntrue; Else return false ; }
6. In addition Apache Common Lang packet Arrayutils class commonly used to connect two arrays, array elements to remove, reverse, time to say ~
Just Jiangzi ~
Java Basics 4--Array of common methods (array)