Java Basics Summary--arrays

Source: Internet
Author: User

The array needs to be mastered:

1, the definition of the array
2, the memory allocation and characteristics of the array
3. Array Operation FAQ
4. Common Array Operations
5. Array in array (understanding)

Array Unique property:length, which is the size of the array.

1. Array definitions

  Format one:

    element type [] Array name = new element type [element number or array length];
Example: int[] arr = new INT[5];
  Format two:

    element type [] Array name = new element type []{element, element, ...};
Example: int[] arr = new int[]{3,5,1,7};
Int[] arr = {3,5,1,7};

Difference: The method defines the length of the array, but does not assign a value to the array, the array has a default value, and the default value of the data type;

Mode two defines the array and assigns a value, specifying the length of the array by default. Recommended Use Method Two

  

2. Memory allocation and characteristics of arrays

 3. Common Array Operations

3.1 Array Traversal

  each data in the array is taken out separately for the for loop to complete the array. The length of the array, that is, the number of stored data, the maximum angular value in the array = array. length-1

1 /*2 iterating through an array that has been resolved with a for loop3 more and more arrays4 How to traverse it?5 In a function, the function of iterating over the array is defined in the function6 the traversal function of the function can be called directly.7 */8 classarraydemo5{9     /*Ten Analysis steps: One This function is responsible for iterating through the array and printing it on the command line A 1. There is no result of the operation, no, void - 2. There is no unknown participation in the function operation, just the array -     */ the  -     /* - the PrintArray function is responsible for iterating through the array, printing the array - When this function is called, the transfer function comes in. +     */ -      Public Static voidPrintArray (int[] arr) +     { A          for(intx = 0; x<arr.length; x + +){ at             if(x!=arr.length-1){ -System.out.print (arr[x]+ ","); -             } -             Else{ - System.out.print (arr[x]); -             } in      -         } to System.out.println (); +     } -      Public Static voidMain (string[] args) the     { *         int[] Arr ={34,435,324,324,3246,63,3}; $         int[] arr1 ={34,435,324,324,3246,63,3,5};Panax Notoginseng PrintArray (arr); -     //PrintArray (arr1); the     } +}

3.2 Array to find the most value

  Iterate over arrays, compare elements in each array, and record the subscript for larger data must define function completion

1 /*2 get the most value action in the array3 maximum value, minimum value4 */5 classarraydemo6{6      Public Static voidMain (string[] args) {7         int[] arr = {4,3,2,6,1,0};8         intMax =Getmax (arr);9 TenSystem.out.println ("max=" +max); One     } A     /* - define the function to get the maximum value in the array - 1 What is the result of a definite function calculation, int the 2 Unknown data is an array -     */ -  -      Public Static intGetmax (int[] arr) { +         //define variables, record the data on the 0 subscript in the array -         intmax = 0;//4 +         //iterate through the array, comparing each, preserving the maximum value A          for(intx = 0; x<arr.length;x++){ at             if(arr[max]<Arr[x]) { -Max =x;  -             } -         } -         returnArr[max]; -     } in  -      Public Static intGetmin (int[] arr) { to         intmin = 0;//4 +         //iterate through the array, comparing each, preserving the maximum value -          for(intx = 0; x<arr.length;x++){ the             if(arr[min]>Arr[x]) *             { $Min =x; Panax Notoginseng             } -         } the         returnArr[min]; +     } A}

3.3 Array sorting

1. Select Sort

? The principle of use is: starting from the No. 0 subscript comparison and a large number of transposition (borrowing third-party variables) after the first comparison, starting from the 1th small index to compare and the larger number of transposition (borrowing third-party variables)

? Features: After the first comparison, the smallest number is now 0 subscript position nested for to complete

2. Bubble sort

Two adjacent data for comparison, transposition according to the result

Two kinds of sorting

3. code example

1         /*2 Three steps to select a sort3 1. Iterate through the array and remove each element of the array separately4 2. After going out, make a comparison of the size5 3. Changing the position of the array6         */7         //Select Sort8          for(intx = 0; x < arr.length-1; X + +) {9              for(inty = x + 1; Y < arr.length; y++) {Ten                 //x=1 y=1+1=2 One                 if(Arr[x] > Arr[y])//Arr[1]>arr[2] A                 { -                     inttemp =Arr[x]; -ARR[X] =Arr[y]; theArr[y] =temp; -                 } -             } -         } +  -         /* + defining functions to implement bubbling sorting A 1. No return value after determining function operation at 2. Indeterminate data is the array -  - Description: - inner Ring Loop,-X is to compare the number of times once in the reduction - 1 is to prevent array out-of-bounds exceptions - Hill Sort, recognized as the most efficient sort of way in the sort algorithm of inserting form -         */ to         //Bubble Sort +          for(intX =0;x<arr.length; X + +){ -             for(inty = 0; Y <arr.length-x-1;y++){ the               if(arr[y]>arr[y+1]){ *                  inttemp =Arr[y]; $Arr[y] = arr[y+1];Panax NotoginsengARR[Y+1] =temp; -               } the            } +}

4. Array Operation FAQ

ArrayIndexOutOfBoundsException array subscript out of bounds exception
NullPointerException NULL pointer exception, most common in reference types

Java Basics Summary--arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.