Array first (2), array first

Source: Internet
Author: User

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:

Related Article

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.