arraycopy example in Java
By: Charles
TheSystem
class has an arraycopy
method that you can
use to efficiently copy data from one array into another:
The twopublic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Object
arguments specify the array to copy from and
the array to copy to. The three int
arguments specify the
starting position in the source array, the starting position in the destination
array, and the number of array elements to copy.
The following program, ArrayCopyDemo
, declares an array of char
elements, spelling the word "decaffeinated". It uses arraycopy
to copy a subsequence of array components into a second array:
class ArrayCopyDemo { public static void main(String[] args) { char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd' }; char[] copyTo = new char[7]; System.arraycopy(copyFrom, 2, copyTo, 0, 7); System.out.println(new String(copyTo)); } }
The output from this program is:
caffein
Archived Comments
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program