[Java] assigns the same value to all elements of the array and copies between arrays.

Source: Internet
Author: User

[Java] assigns the same value to all elements of the array and copies between arrays.


Assign the same value to all elements of the array:

boolean[] resArray=new boolean[100]; Arrays.fill(resArray, true);


Copying between Arrays:

System.arraycopy(Object src, int srcPos, Object dst, int dstPos, int length)

Src: source array; srcPos: Start position of the source array to be copied; dest: Destination array; destPos: Start position of the destination array; length: Copy length.

Note: Both src and dest must be of the same type or an array that can be converted. Interestingly, this function can be used to copy data on its own,

For example: int [] fun = {0, 1, 2, 3, 4, 5, 6}; System. arraycopy (fun, 0, fun, 3, 3); the result is: {, 6 };


JAVA programming array replication, assigning the first five elements of array a1 to b1

Public class SetArray {
Public static void main (String [] args ){
Int [] a = {1, 2, 3, 4, 5, 6, 7 };
Int [] B = {1, 1, 8, 9, 10, 11, 12 };
For (int I = 0; I <5; I ++ ){
B [I] = a [I];
System. out. println (B [I]);
}
}
}

How does Java attach elements of a string array to another string array?

ArrayCopy ();
Public static void arraycopy (Object src,
Int srcPos,
Object dest,
Int destPos,
Int length): copy an array from the specified source array. Copy starts from the specified position and ends at the specified position of the target array. From the source array referenced by src to the target array referenced by dest, A subsequence of the array component is copied. The number of the copied component is equal to the length parameter. Components located between srcPos and srcPos + length-1 in the source array are copied to destPos and destPos + length-1 in the target array respectively.
CopyOf () and copyOfRange () of java. util. Arrays class ();
Public staic xxx [] copyOf (xxx [] original.int newLength)
Original is an array reference of any type. It refers to the source array. newLength is the length of the test bay.
Public static xxx [] copyOfRange (xxx [] original, int from, int)
Original is an array reference of any type. It refers to the source array, from test bay to-1.

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.