1. Arrays
program = algorithm + data structure;
The if...else ..., switch ..., while loops, for loops, and so on, are all problems in solving the process, that is, the algorithm.
Data structure: It is to be stored according to a certain structure, and the array is the most basic data structure.
1) Definition of the array:
A collection of elements of the same data type that can only hold elements of the same data type.
The elements of an array are arranged in linear order, that is, one by one.
Array elements can be identified by ordinal (subscript), the subscript starts at 0, and the maximum subscript is the length of the array-1.
2) Declaration of the array:
Declares an integral array of arr, containing 3 elements, each of which is of type int
int[] arr = new INT[3]; each element in the//arr is defaulted to 0
array variable arr is no longer a basic type variable, but a reference type, referencing a memory address that contains an array element.
3) Initialization of the array:
int[] arr = {1,4,7}; Static initialization, which can only be assigned directly while declaring a variable
Int[] arr;
arr = new Int[3]; Dynamic initialization with element assignment in sequence
int[] arr = new int[]{1,4,7}; Dynamic initialization, which can be assigned directly at the time of definition
4) Array of access:
Access elements by subscript, subscript starting from 0, to arr length (lengths)-1
Int[] arr = {1,4,7,8};
System.out.println (arr.length); Length 4
ARR[0] = 100; Assigns a value of 100 to the 1th number in arr
System.out.println (Arr[3]); Output the last number in arr
ARR[4] = 88; Wrong, subscript out of bounds/over range
//loop to assign values to array elements
for (int i=0;i<arr.length;i++) {
Arr[i] = 100;
}
Loop output Array
for (int i=0;i<arr.length;i++) {
System.out.println (Arr[i]);
}
Reverse output array
for (int i=arr.length-1;i>=0;i--) {
System.out.println (Arr[i]);
}
Case 25:
650) this.width=650; "style=" width:558px;height:514px; "title=" CJI ' ZC5 "j_ww.png" src= "Http://s3.51cto.com/wyfs02 /m01/6d/17/wkiom1vcacrh5tdzaakju7ywkiq800.jpg "width=" 579 "height=" 544 "alt=" wkiom1vcacrh5tdzaakju7ywkiq800.jpg "/ >
6) array type element default value
Depending on the array type, the default initialization value is: 0 (integer), 0.0 (floating point), False (Boolean type),
\u0000 (char character type, showing no effect, equivalent to a space), null (string type, nothing, null meaning).
2. Array Tool class arrays
The tool classes provided by the JDK to manipulate the array provide some common methods.
Int[] arr = {1,4,7,8};
1) output of the array
String str = arrays.tostring (arr);
2) Copying of arrays:
Note: arrays.copyof (source array, new length), returns the new array
When the new length equals the length of the source array, it is copied;
When the new length is greater than the length of the source array, the content of the preceding content is the source array, and the contents are filled with the default value of the array element type;
When the new length is less than the length of the source array, interception, starting from subscript 0 to intercept a certain new length of elements;
Case 26:
650) this.width=650; "style=" width:665px;height:515px; "title=" X%[8h8xcrx$9.png "src=" Http://s3.51cto.com/wyfs02 /m02/6d/12/wkiol1vcacpsqomgaansmrb_g2o292.jpg "width=" 680 "height=" 528 "alt=" wkiol1vcacpsqomgaansmrb_g2o292.jpg "/ >
Output Result:
650) this.width=650; "title=" 26~m[w ' Ryl}o_7.png "src=" http://s3.51cto.com/wyfs02/M00/6D/12/ Wkiol1vcaebyvfjcaadncrsw9dq968.jpg "alt=" Wkiol1vcaebyvfjcaadncrsw9dq968.jpg "/>
*************************************************************************************************************
Systems system also provides an array of replication methods:
System.arraycopy (object src, int srcpos, object dest, int destpos, int length);
SRC: source array; Srcpos: The starting position where the source array is to be copied;
Dest: target array; Destpos: The starting position of the target array placement;
Length: The size of the copy.
The indexoutofboundsexception exception is thrown and the target array is not modified as long as any of the following conditions are true:
The Srcpos parameter is negative.
The Destpos parameter is negative.
The length parameter is negative.
Srcpos+length is greater than src.length, which is the length of the source array.
Destpos+length is greater than dest.length, which is the length of the destination array.
*********************************************************************************************************** **
Case 27:
650) this.width=650; "style=" width:667px;height:415px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6d/17/wkiom1vcaiutih0oaakvatcbfa4807.jpg "width=" 739 "height=" 463 "alt=" wkiom1vcaiutih0oaakvatcbfa4807.jpg "/ >
3) sort the array:
Arrays.sort (arr), no return value
Case 28:
650) this.width=650; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/M02/6D/17/ Wkiom1vcal6ajlnpaajedsdx3c8458.jpg "alt=" Wkiom1vcal6ajlnpaajedsdx3c8458.jpg "/>
Output Result:
650) this.width=650; "title=" 28clipboard.png "src=" http://s3.51cto.com/wyfs02/M00/6D/12/ Wkiol1vcakvszbvbaab09vuluom292.jpg "alt=" Wkiol1vcakvszbvbaab09vuluom292.jpg "/>
3. Exercises
Case 29:
650) this.width=650, "style=" WIDTH:659PX;HEIGHT:544PX; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M00/6d/12/wkiol1vcamfyj21yaal6b6ny_dy123.jpg "width=" 699 "height=" 565 "alt=" wkiol1vcamfyj21yaal6b6ny_dy123.jpg "/ >
Case 30:
650) this.width=650; "style=" width:689px;height:516px; "title=" Clipboard.png "src=" http://s3.51cto.com/wyfs02/ M01/6d/12/wkiol1vcaoectiivaapjzttii8o843.jpg "width=" 956 "height=" 569 "alt=" wkiol1vcaoectiivaapjzttii8o843.jpg "/ >
This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1653094
Java Basic Learning Notes--9 (array)