Dark Horse Programmer-java Basics-array & binary conversions

Source: Internet
Author: User

First Lecture Array Initialization & Operation

1. Array definition

Concept: An array is a collection of data of the same type, in fact an array is a container. The elements in the array are numbered starting with 0, which makes it easier to manipulate the elements (get, modify, sort, etc.).

The format of the array definition is:

int[] arr = new INT[10]; --this specifies the length of the array, and the subsequent need to assign a value to each element;

int[] arr = new int[]{2,1,5,9};--Create an array entity and initialize it without specifying a length;

Int[] arr = {2,1,5,9};--defines the array and initializes it;

2. Array manipulation

1) Gets the elements in the array, which usually needs to traverse the array

Int[] arr = {2,1,5,9};

for (int x = 0;x<arr.length;x++)//length is an attribute in the array that represents the number of array elements.

{

System.out.print (arr[x]);//Iterate through the array arr, printing all the elements in the array.

}

2) Get the maximum value

Get maximum value idea : 1, get the most value needs to compare, each comparison will have a large value, you can put the larger value in a temporary variable. 2. Make a comparison between each element in the array and the value in the variable, and assign the element value to the variable if it is greater than the value in the variable. 3. When all elements are compared, the value in the variable is the maximum value in the array.

steps:1, the definition of variables, initialized to any one of the elements in the array, 2, through the loop to iterate through the group of statements, 3, in the process of defining the judgment condition, if the traversed element is larger than the value of the variable, the element is assigned to the variable.

get the minimum idea: similar to getting the maximum value, only the smaller value is stored in the variable when the array is traversed for comparison.

The Get maximum minimum value code is implemented as follows:

Second lecture Array Sorting

1. Select the Sorting method

Inside the loop once, the maximum value appears in the position of the header

Idea: Take the first element of the array and the following elements of the traversal comparison, if greater than (less than) the first is the value of the interchange, the first element and other elements after the comparison, take the second element and the following elements of the traversal comparison, and so on, until the completion of the loop, the array is sorted.

The implementation code is as follows:

2. Bubble Sort Method

Inside the loop once, the maximum value appears in the last corner of the label position.

Idea: The adjacent two elements are compared, if the matching conditions (greater than or less than) to exchange values;

The code is implemented as follows:

3. Hill sort

The hill algorithm is the fastest sorting algorithm.

The reason for the low efficiency of the Select/bubble sorting method is that the element swap in heap memory is required after the comparison is complete. Solution: Place the pin of the element that needs to be transposed in memory, and the last interchange is the direct array element interchange.

4, Java has a self-contained sorting method: Arrays.sort (arr);

Third Speaking Array Lookup Operations

1. Gets the position where the key value first appears in the array

Idea: Iterate through the array and compare it to that value, and return the position of the corner label if it is equal.

The code is implemented as follows:

2. Binary search algorithm

Binary lookups can reduce the number of traversal times, which increases the efficiency of lookups, but requires that the array be ordered.

Idea: Compare the key values with the intermediate elements of the data, then determine the key values in the first half or the second half of the comparison, and then binary find the part, and so on.

The code is implemented as follows:

3. Practice

Inserts an element into an ordered array to ensure that the array is still ordered. If you get the element where the array should be inserted.

Ideas:

The binary algorithm finds the element in the array and returns the minimum subscript, which is where the element is inserted into the array.

Fourth Lecture Binary Conversion

1, decimal into binary

2, decimal into hexadecimal

3, check the table method to convert decimal into hexadecimal

Idea: the hexadecimal element (0~f) is stored temporarily in a table, and each time the value after &15 as the index area to find the established table, you can find the corresponding element.

The code is implemented as follows:

4, check table method will be decimal into binary

Summary of Knowledge points

1. Arrays have multiple definitions and initialization methods

1) If you do not know the value of the array, but until the length of the data, you can define the data and specify the length: int [] arr = new INT[10], which can only be traversed to assign the element to a separate position in the array;

2) If the array length is not known, but until the value in the array, it can be defined and initialized in this way: int [] arr = new int[]{1,2,3}

2. Array sorting

All need to take advantage of nested loops.

1) Select the sorting method, the inner loop once, the most value appears in the position of the mark of the Tau Kok;

2) Bubble sorting method, the inner loop once, the most value appears in the last corner of the mark position;

3) Hill ranking is the most efficient;

4) There is a self-contained sequencing program in Java: Arrays.sort (arr);

3. Find

To find the first occurrence of a keyword in an array, the binary algorithm is more efficient.

4. Binary conversion

By taking the remainder to get the value of a bit, it can be converted by looking up the table method, and it is convenient to take the values.

Dark Horse Programmer-java Basics-array & binary conversions

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.