------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
A concept array is a collection of data of the same type, a container, a reference type, stored in a heap. Benefit: You can automatically number the elements in the array starting with 0 to facilitate the operation of these element formats: 1. int[] Arr=new int[5]; Creates an array of length 5 with the variable named arr, which is of type Integer. 2. int[] arr=new []{5,6,8,3,4} int[] arr={5,6,8,3,4} common problems in arrays: 1, array angle label out of bounds//arrayindexoutofboundsexception2, null pointer exception//nu Llpointerexception when a reference does not have any point, the value is empty, and the reference is also used to manipulate the entity. Array operations: 1. Gets the data in the array, usually using the traversal (for loop), arr.length the length code of the array: int[] arr=new int[5]; for (int x=0;x<arr.length;x++) {arr[x]; }2. Prints the elements in the array, separated by commas between the elements. Int[] Arr=new int[5]; for (int x=0;x<arr.length;x++) {if (x!=arr.length-1) System.out.print (arr[x]+ ","); else System.out.print (arr[x]); }3. Get the value//define a method that returns the maximum value of the corner label public static int Getmax (int[] arr) {int max=0;//define a variable initialization number The 0-point value of the group for (int x=0; x<arr.length;x++) {//loops through the array if (Arr[max]<arr[x]) {///if the temporary element is less than the traversed element max=x;//the corner label of the value to be traversed to the temporary variable corner label }}return max;//returns the maximum value of the}4. Array sort-Select sort: The inner loop ends once, the maximum value appears in the position of the angle of the header, the first one in turn and the following several compared to the public static void Xuanze (int[] arr) {for (int x=0; X≪arr.length-1;x++) {//control the number of laps to compare for (int y=x+1;y<arr.length;y++)//control the number of times per lap comparison {if (Arr[x]>arr[y]) {int temp=arr[y]; arr[y]=arr[x];arr[x]=temp;}}} 5. Array sort-bubble sort: The next two elements are compared, the first lap, the highest value appears in the last. public static void Maopao (int[] arr) {for (int x=0; x<arr.length-1;x++) {//control comparison of the number of laps for (int y=0;y<arr.length-x-1;y++ //control the number of times per lap comparison-1 is to avoid the corner mark out of Bounds,-X is to let each comparison of the element reduce {if (arr[y]>arr[y+1]) {int temp=arr[y+1];arr[y+1]=arr[y];arr[y]=temp;}}} 6. Array substitution public static void Maopao (int[] arr,int x, int y) {int temp=arr[x];arr[x]=ar R[y];arr[y]=temp; }7. General lookup of an array: Gets the position of the first occurrence of the key in the array, if 1 is returned, there is no public static int select (int[] arr,int key) {for (int x=0; x<arr.length;x++) {if (Arr[x]==key) {return x;}} return-1;} 8. Array binary lookup: Can improve the search efficiency, but must be an ordered array the first way: first to find the head and tail corner of the mark min = 0////Corner Mark max=arr.length-1;//Tail angle superscript mid = (Min+max)/2;//The median angle is the value to be found and When the angular marks are not equal, the loop is searched. If the value to be found is larger than the median angle, the angle mark is the median +1; if the value to be looked for is smaller than the median angle, the tail angle is the middle angle label-1; intermediate angle label re-assigned mid = (Min+max)/2;//Intermediate angle label if the corner mark is larger than the tail angle label, There is no such value in the array, and the second way: If there is a distance between the angle mark and the tail point mark, that is, the angle mark and the tail corner are less than or equal to the tail angle mark, then the binary is carried out. Otherwise there is no return-1 mid = (Min+max)/2;//after the middle angle mark then if the value to be looked for is larger than the median angle, the angle mark is the middle corner mark +1; If the value to be found is smaller than the median angle, the tail angle is the middle corner mark-1, and if equal, returns the median value. Found an interview: In an ordered array, insert a value, and after inserting, the array is guaranteed to be ordered. Gets the position of the element in the array. The value of the smallest angle is the position of Min.
Dark Horse Programmer--java Basics-Arrays