Java study prelude daysof04

Source: Internet
Author: User

1.1.1. Array

Arrays are collections of the same data type. arrays are actually containers. In simple terms, arrays are used to store things and things are elements. The benefits of arrays can be automatically numbered from 0 to facilitate operations on these elements. What is the array format?

 

Element type [] array name = new element type [number or length of elements]

 

New is used to create a data-type container entity in the memory.

1.1.2. Array Operations

Array Operations usually get the value of the elements of the array, and traverse is used. The traversal will use an attribute length of the array.

Array Operations to find the maximum value of the array, through the use and application of the For Loop, then can be said to use the algorithm, bubble sort

1.1.3. Sorting of Arrays

Bubble sorting method:

 
Public class sort {public static void main (string [] ARGs) {int [] arr = {5, 1, 6, 4, 2, 8, 9}; printarray (ARR); system. out. println ("inline"); bubblesort (ARR); printarray (ARR);} public static void bubblesort (INT [] ARR) {If (ARR = NULL | arr. length = 0) {Throw new illegalargumentexception ("the array is not initialized. ") ;}for (INT I = 0; I <arr. length-1; I ++) {for (Int J = 0; J <arr. length-1-i; j ++) {//-x reduce the elements of each comparison, -The purpose of-1 is to avoid subscripts crossing the border if (ARR [J]> arr [J + 1]) {int temp = arr [J]; arr [J] = arr [J + 1]; arr [J + 1] = temp ;}}} public static void printarray (INT [] ARR) {If (ARR = NULL | arr. length = 0) {Throw new illegalargumentexception ("the array is not initialized. ");} system. out. print ("["); For (INT I = 0; I <arr. length; I ++) {if (I! = Arr. length-1) system. out. print (ARR [I] + ","); else system. out. print (ARR [I] + "]") ;}}

Select sorting method

Program code:

 
Public class sort {public static void main (string [] ARGs) {int [] arr = {5, 1, 6, 4, 2, 8, 9}; printarray (ARR); system. out. println ("\ n ---------------------------"); selectsort (ARR); printarray (ARR);} public static void selectsort (INT [] ARR) {If (ARR = NULL | arr. length = 0) {Throw new illegalargumentexception ("the array is not initialized. ") ;}for (INT I = 0; I <arr. length-1; I ++) {for (Int J = I + 1; j <arr. len Dependencies; j ++) {If (ARR [I]> arr [J]) {int temp = arr [I]; arr [I] = arr [J]; arr [J] = temp ;}}} public static void printarray (INT [] ARR) {If (ARR = NULL | arr. length = 0) {Throw new illegalargumentexception ("the array is not initialized. ");} system. out. print ("["); For (INT I = 0; I <arr. length; I ++) {if (I! = Arr. length-1) system. out. print (ARR [I] + ","); else system. out. print (ARR [I] + "]") ;}}

 

From the Bubble sorting method and the selection sorting method, we can see that some code is duplicated, that is, the position replacement, so to simplify the code, we can extract a function for this part of the replacement position:

 

Public static void swap (INT [] arr, int A, int B ){

 

Int temp = arr [a];

 

Arr [a] = arr [B];

 

Arr [B] = temp;

 

}

 

 

1.1.4. Half-lookup operation

As the name suggests, a half-fold query means that only half of each query is performed, and such operations are repeated, which improves the search efficiency compared with the usual search methods, however, to use a half-fold search, the array must have been sorted. The following describes common code writing:

 
Public class harfsearch {public static void main (string [] ARGs) {int [] arr = {, 9, 15,}; system. out. println (generalsearch (ARR, 215);} // common search method public static int generalsearch (INT [] arr, int key) {If (ARR = NULL | arr. length = 0) {Throw new illegalargumentexception ("array not initialized");} For (INT I = 0; I <arr. length; I ++) {If (ARR [I] = Key) {return I ;}} return-1 ;}}

The code of the half-fold algorithm is:

 
Public class harfsearch {public static void main (string [] ARGs) {int [] arr = {,}; // system. out. println (generalsearch (ARR, 215); system. out. println (harfsearch (ARR, 22);} // semi-query public static int harfsearch (INT [] arr, int key) {If (ARR = NULL | arr. length = 0) {Throw new illegalargumentexception ("array not initialized");} int start = 0, end, mid; end = arr. length-1; Mid = (start + end)/2; whil E (ARR [Mid]! = Key) {If (ARR [Mid]> key) {end = mid-1 ;}else {start = Mid + 1 ;}if (Start> end) {return-1 ;}mid = (start + end)/2 ;}return mid ;}}

 

1.1.5. Memory Structure

First, you must know that to run any program, you must open up a space in the memory. The reason for sharding is that the data processing methods of each slice are different. in Java, the memory is divided into five districts, stack memory, heap memory, method zone, local method zone, and register. The stack stores all local variables. When the variables are used up, the space is automatically released. For example, if int I = 3, the memory of I is in the stack. Int [] arr = new int [3]; it is divided into int [] arr and ARR = new int [3]. The local variable arr is defined in the stack memory, newint [3] will open up a space in the heap memory so that arr points to it, pointing to the address value of the array in the heap memory. Therefore, the storage of arrays is separated. Similar to this, there are classes and objects. Int []
Arr = new int [] {1, 2, 3, 4}; this is called static initialization of arrays. This is an array initialization method, and a common initialization method, int [2] = new int [2]; // The Initialization is: arr [0] = 1; arr [1] = 2;
Supplement: Here there are two exceptions: ① arrayindexoutofboundsexception, array out of bounds; ② nullpointerexception NULL pointer exception, indicating that the variable has no initialization value of null.

 

PS: Upload the Java Study Notes for the fourth day. The basics are over, and the next step is the intermediate ones. Come on!

Thank you for your correction!

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.