Well off with you to learn JAVA -------- array copy operations, java -------- Array

Source: Internet
Author: User

Well off with you to learn JAVA -------- array copy operations, java -------- Array

The API provides many methods for developers to use. The next two articles will share with you two common array operation methods. Today we will introduce the first method, the copy operation of arrays.

Example: TestJava4_4.java

01 // The following program describes the copy operation of the array

02 public class TestJava4_4

03 {

04 public static void main (String [] args)

05 {

06 int a1 [] = {1, 2, 3, 4, 5}; // declare two integer arrays a1 and a2 and perform static Initialization

07 int a2 [] = {9, 8, 7, 6, 5, 4, 3 };

08 System. arraycopy (a1, 0, a2,); // you can copy an array.

09 System. out. print ("a1 array content :");

10 for (int I = 0; I <a1.length; I ++) // output the content in the a1 Array

11 System. out. print (a1 [I] + "");

12 System. out. println ();

13

14 System. out. print ("a2 array content :");

15 for (int I = 0; I <a2.length; I ++) // output the content in the a2 Array

16 System. out. print (a2 [I] + "");

17 System. out. println ("\ n Array copied! ");

18}

19}

 

 

Output result:

A1 array content: 1 2 3 4 5

A2 array content: 1 2 3 6 5 4 3

Array copied!

 

System. arrayCopy (source, 0, dest, 0, x): The statement means to copy the x elements of the source array starting from subscript 0 to the target array, access from the position corresponding to the subscript 0 of the target array.


Java array copy problem u !!!

Public static void main (String [] args ){
Int [] a = {1, 2, 4, 5, 6, 7, 8, 9, 10 };
Int [] B = new int [10];
// Copy the elements in array a to array B and print the elements in array B.
For (int I = 0; I <a. length; I ++ ){
B [I] = a [I];
System. out. println (B [I]);
}
// Add the new data 200 to the eighth position in array.
Int [] temp = new int [11];
For (int I = 0, j = 0; I <temp. length; I ++, j ++ ){
If (j = 7 ){
Temp [I] = 200;
I ++;
}
Temp [I] = a [j];

}
A = temp;
For (int I = 0; I <a. length; I ++ ){
System. out. println (a [I]);
}
// Delete the data at the fifth position in the array after adding the new data, and print the element order in the array.
For (int I = 0; I <a. length-1; I ++ ){
If (I> = 4 ){
A [I] = a [I + 1];
}
System. out. println (a [I]);
}
}

Java copies the value of an array to another array. The copy method is not allowed.

/** Copy the src array as the source array. the dest array is the target array */
Public static void arrayCopy (Object [] src, Object [] dest ){
If (src = null | dest = null) {throw new NullPointerException ("both the source array and target array cannot be empty ");}
If (! Dest. getClass (). getComponentType (). isAssignableFrom (src. getClass (). getComponentType ())){
Throw new RuntimeException ("Type mismatch ");
}
If (dest. length <src. length ){
Dest = (Object []) java. lang. reflect. Array. newInstance (dest. getClass (). getComponentType (), src. length );
}
For (int I = 0; I <src. length; I ++ ){
Dest [I] = src [I];
}
}

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.