Array first (2), array first
I. Maximum algorithm:
1. Assume that the first element in the array is the maximum value.
Int max = arr [0]; // max is the maximum value.
2. traverse the remaining elements in the array cyclically
For (int I = 1; I <arr. length; I ++ ){
3. array element to max ratio
If the array element is greater than max, Set max to an array element.
If (arr [I]> max ){
Max = arr [I];
}
}
Sample Code:
II. Array replication:
1. Use the System. arraycopy () method to copy arrays.
Public static void arraycopy (Object src, int srcPos, Object dest, int destPos, int length)
Src-source array
SrcPos-start position in the source array
Dest-target array
DestPos-start position in the target array
Length-Number of array elements to be copied
2. The Arrays. copyOf method is used for Array replication.
Use the copyOf method of the java. util. Arrays class to copy Arrays.
Type [] newArray = Arrays. copyOf (type [] original, int newLength );
Feature: copies of the original array when the new array is generated
NewLength is smaller than the source array.
If newLength is greater than the source array, it is filled with 0 or null.
Therefore, the new array can be larger than the source array length.
Note:
Array resizing
The length of the array cannot be changed after it is created. The so-called expansion refers to creating a larger new array and copying the content of the original array to it.
You can use the Arrays. copyOf () method to easily extend the array.
Sample Code:
Running result:
Sample Code:
Running result:
Sample Code:
Sample Code: