Summary of common operations of arrays based on Java (must see)

Common operations on arrays

1. Find the maximum value and minimum value in the array

Idea: assuming that the element with subscript 0 is the maximum value, traverse the array and compare it with Max in turn. If any element is larger than this max, assign this value to max. The minimum value is the same

2. Finds whether an element exists in the array

(2) . use the binary search method to find whether there is an element in the array

Premise: the array to be found must be ordered (ordered in size)

Principle: compare the element to be found with the element of the middle subscript in the array. If it is larger than the middle element, go to the right to find it. If it is smaller than the middle element, go to the left to find it.

3. Sort arrays

(1) Bubble sort

Principle: compare adjacent elements, small ones move forward, large ones move back, and the maximum value appears at the maximum index

Analysis: when comparing for the first time, the large one will appear later, and the maximum value will be ranked at the maximum index

For the second comparison, because the maximum value has been determined, only the first n-1 elements need to be compared to determine that the second largest value is ranked at the second largest index

Determine the third largest value and the fourth largest value in turn

Conclusion: n numbers to queue up, two are relatively small, front, outer cycle n-1, inner cycle n-1-i

(2) Select and sort

Principle: start from the subscript 0 and compare with the following elements in turn. If the latter element is less than the subscript 0, transpose. Compare the new element with subscript 0 with the following one. After the first time, the minimum value appears at index 0

Example: {10, 3, 8, 1, 6}

In the first round of comparison, start with the subscript element of 0 and compare with the following elements in turn. First, compare 10 and 3. If 10 < 3, exchange the position, and the element of subscript 0 becomes 3, {3, 10, 8, 1, 6}; Compare 3 and 8, 3 < 8, no transposition; 3 and 1 are compared, 3 > 1, transposition

{1, 10, 8, 3, 6}, then compare 1 and 6, 1 < 6, do not change the position. End of the first round, {1, 10, 8, 3, 6}

In the second round of comparison, the element of subscript 0 has been determined as the minimum value in the previous round. This round of comparison starts with subscript 1. First, 10 and 8 are compared and transposed {1, 8, 10, 3, 6}; 8 and 3 comparison, transposition {1, 3, 10, 8, 6}, 3 and 6 comparison, no transposition. At the end of the second round, determine that the penultimate element is at the subscript 1 position.

........

A total of length-1 rounds were compared.

4. Delete elements in array

(1) Delete the element according to the subscript (fill 0 in the blank)

(2) Delete the corresponding element in the array according to the input element

The following describes some common operations on arrays in the API

In Java, except Java The classes and interfaces under Lang package can be used directly, but the classes or interfaces under other packages need pilot packages.

java. util. Arrays class: this class contains various methods for manipulating arrays, such as sorting and searching.

These are static methods, which can use class names The method name is used directly. Here, take the int array as an example

1. Quick sort arrays

Arrays. sort(int[] arr); Sort the incoming array in ascending order by default

2. Returns a string representation of the contents of the specified array.

Arrays. toString(int[] arr);

3. Use dichotomy search to specify the index of an element in the array

Arrays. binarySearch(int[] arr);

4. Assigns the specified int value to each element of the specified int array.

Arrays. fill(int[] arr,int val);

5. Copies the specified array, intercepts or fills with 0 if necessary, so that the copy has the specified length.

Arrays. copyOf(int[] arr,int newLength); Its return value is an array

6. Copies the specified range of the specified array to a new array. Contains the start position but not the end position.

Arrays. copyOfRange(int[] arr,int from,int to); Its return value is an array

Additional array knowledge:

1. Command line parameters: you can pass in parameter values for the main method when executing Java commands.

Usage: pass in command line parameters when running Java command: Java class name "value 1" and "value 2"

Public static void main (string [] args) {}, we can see that the main method is a method with parameters, and the parameters are a string array. When the command line passes values to the main method, the passed values are saved in the args character array.

Note: multiple parameter values are separated by spaces. The value of the parameter will be saved to the string array, passed into the main method, and the subscript starts from zero.

When obtaining command line parameters, it should be noted that the subscript cannot exceed the limit, and the maximum subscript should be the number of parameters - 1

2. Variable parameters

The variable parameter is java 1 The new feature after 5 can represent zero to multiple variables of the same data type, which is to solve the problem of excessive method overloading caused by the change of the number of parameters.

be careful:

1. Variable parameters can only be used for formal parameters (when defining methods). Variable parameters can be treated as arrays.

2. A method can only have one variable parameter at most, and the variable parameter must be the last parameter.

3. When calling a method with variable parameters, the data type must correspond to the type of variable parameters.

Example:

Merge array operation: there is an existing array as follows: int oldarr [] = {1,4,5,6,7,5} it is required to remove the items with a value of 0 in the above array, and store the values that are not 0 into a new array. The generated new array is: int newarr [] = {1,5}

Idea: determine the number that is not 0, so you can open up a new array; Take the content from the old array and assign it to the newly opened array.

2. Use dichotomy to find elements in an ordered array. Found return index, no output - 1. Using recursive implementation

The above summary of common operations of arrays based on Java (must see) is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>