An array is a data structure that stores a collection of values of the same type, or a set of data that is consistent with the data type.
First, syntax and declaration arrays
1. Syntax: data type [] array = new data type [length];
Attention:
When creating an array with length, each element has a default value.
for int, byt, short, long, the default value is 0;
For double, float, the default value is 0.0;
For a Boolean value, its default value is false;
For char, the default value is the character encoded as 0;
For reference types (including string), the default value is null.
2, if you already know the elements of the array, you can directly use elements to initialize the array, but do not need to write the array length, the number of elements is the array length. Second, for example int[] A and int a[] Both declarations are available, but it is not recommended to use the following notation.
3, if the data elements are not many, and know the value of each element, you can directly use the element to initialize. If there are too many elements, create an array with length. If the element has no initial value, use length initialization.
/*** Declaration of an array*/ Public Static voidTestMethod () {//The first form of a notation int[] Array =New int[3]; //The default value for type int is 0System.out.println (Array[0]);//0string[] Strarray =NewString[3]; //To a reference type (which includes a string) with a default value of NULLSystem.out.println (Strarray[0]);//NULL//the second way of declaring an array int[] Array2 = {}; System.out.println (array2[0]);//1//This is also possible, but it is not recommended to use the [] immediately following the data type, understanding and distinguishing intArray3[] = {4,5,6}; System.out.println (array3[0]);//4}
Second, access to array elements
1. Use the array name [subscript ordinal] to access the array element.
2. The array's. Length property allows you to get the length of the array, that is, the number of array elements.
3, array subscript range is from 0 onwards, until length-1.
4, if the super-range access, there will be a run exception, array subscript out of bounds. Subscript less than 0 or greater than or equal to length, is a super range.
/*** Array length acquisition, element access*/ Public Static voidarraylength () {//gets the length of the array, using the. Length Property int[] Array = {1,2,3,4}; System.out.println (Array.Length); //4//once the length of the array is defined, it cannot be changed because its length property is modified by the final keyword and cannot be modified//array.length = 5;//The final field Array.Length cannot be assigned//accessing the elements of an array, using the [element subscript], subscript starting from 0System.out.println (Array[0]);//prints the first element of an array 1//java.lang.ArrayIndexOutOfBoundsException exception occurs when subscript is out of bounds//System.out.println (array[4]);}
Three, array traversal
With the For loop and for Each loop, it is easy to iterate through the array elements.
/*** Array Traversal*/ Public Static voidseearrayelements () {int[] Array = {1,2,3,4,5}; //For loop traversal printing for(inti=0; i<array.length; i++) {System.out.println (array[i]); } //For each traversal of the print, the first parameter in parentheses is the data type of the element//The second parameter is the name of the local variable that gets the value (optionally named), and the third argument is the array name for(intElement:array) {System.out.println (element); } }
Iv. Array Copy and expansion
1. Copy of the array.
Using the CopyOf () method in the Java.util.Arrays class, and the Arrays.copyof () method is implemented by using the Arraycopy method of the system class, interested in trying to build the wheel, Then compare it with the official Arraycopy method.
2, array expansion.
Once an array is initialized, its length cannot be modified, so it is possible to make an array length larger or smaller, as well as with the arrays.copyof () method. Whether the array is expanded or smaller, the original array does not change, but the object is replaced by a new array.
/*** Copy array, array expansion, whether the array is expansion or smaller, the original array will not change*/ Public Static voidmakearraycopy () {//Copy the array, here is the copyof () method in the Java.util.Arrays class int[] Array = {2,4,6}; int[] NewArray =arrays.copyof (array, array.length); //determines whether two arrays are equal, using the Equals () method in the Arrays classSystem.out.println (arrays.equals (array, newarray));//true//Array Expansion int[] Addlength = arrays.copyof (Array, Array.Length + 1); System.out.println (addlength.length); //4 for(inte:addlength) {System.out.println (e);//Print 2 4 6 0 sequentially } //array becomes smaller int[] AddLength2 = arrays.copyof (Array, array.length-1); System.out.println (addlength2.length); //2 for(inte:addlength2) {System.out.println (e);//Print 2 4 } }
Five, array sorting
The adjacent elements of an array are compared and replaced by ascending or descending rules, and the sorted array is finally obtained.
/*** Ascending sorting of arrays*/ Public Static voidSortarrayasc () {//Declaration and initialization of an array int[] Array = {2, 10, 3, 78, 1, 29, 91, 5}; System.out.println (arrays.tostring (array)); for(inti=0; i<array.length-1; i++) { for(intj=0; j<array.length-i-1; J + +) { if(Array[j] > array[j+1]) { intK =Array[j]; ARRAY[J]= Array[j+1]; Array[j+1] =K; }}} System.out.println (Arrays.tostring (array)); } /*** Descending sorting of arrays*/ Public Static voidSortarraydesc () {//Declaration and initialization of an array int[] Array = {2, 10, 3, 78, 1, 29, 91, 5}; //Turn an array into a string outputSystem.out.println (arrays.tostring (array)); for(inti=0; i<array.length-1; i++) { for(intj=0; j<array.length-i-1; J + +) { if(Array[j] < array[j+1]) { intK =Array[j]; ARRAY[J]= Array[j+1]; Array[j+1] =K; }}} System.out.println (Arrays.tostring (array)); } /*** Sorting using the Arrays.sort () method*/ Public Static voidUtilsort () {//Declaration and initialization of an array int[] Array = {2, 10, 3, 78, 1, 29, 91, 5}; System.out.println (arrays.tostring (array)); Arrays.sort (array); System.out.println (arrays.tostring (array)); }
Six or two-D arrays
Two-dimensional array, in essence, is a one-dimensional array, the operation of the two-dimensional array is actually a one-dimensional array operation.
1. Declaration. Type [] Variable name = new Type [length] [length];
2, Length property. Its length is the length value in the first [].
3, subscript is also starting from 0, but also follow the subscript can not cross the rules.
4, other operations are similar to the operation of a one-dimensional array.
Public Static voidStatetwodimarray () {//declaring a two-dimensional array int[] Array =New int[2] [3]; //assigning values to its elementsArray[0][0] = 1; array[0][1] = 2; array[0][2] = 3; System.out.println (arrays.tostring (array[0]));//[1, 2, 3]System.out.println (arrays.tostring (array[1));//[0, 0, 0]System.out.println (Array.Length);//2//declares and initializes a two-dimensional array int[] Array2 = {{1,2},{3,4}}; System.out.println (arrays.tostring (array2[0]));//[1, 2]System.out.println (arrays.tostring (array2[1));//[3, 4]System.out.println (array2.length);//2}
The above code is uploaded to GitHub, the address is https://github.com/XiaoChuan94/javalearning/tree/master/javalearningday05, there is a need to download to watch, If you like it, give it to a star! If there is insufficient, welcome the message exchange below.
The article starts with my personal public number: Yue Lok book . I like to share the songs I've heard, the movies I've seen, the books I've read, the code I've been reading, the deep-night meditation. Look forward to your attention!
Public number backstage Enter the keywords "java learning ebook ", you can get 12 Java learning related e-book resources, if the economic capacity allows, but also to support the book author of the original paper books, creation is not easy.
Getting Started with Java (vi): arrays