The System.arraycopy()
method in Java is used to copy a specified range of elements from one array to another.
Table of Contents
- Introduction
arraycopy()
Method Syntax- Examples
- Basic Usage
- Handling Overlapping Arrays
- Copying Part of an Array
- Error Handling
- Real-World Use Case
- Conclusion
Introduction
The System.arraycopy()
method is a static method of the System
class in Java. It provides a fast and efficient way to copy elements from one array to another. This method is useful for various array manipulations, such as resizing arrays, merging arrays, or shifting elements.
arraycopy() Method Syntax
The syntax for the arraycopy()
method is as follows:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Parameters:
src
: The source array.srcPos
: The starting position in the source array.dest
: The destination array.destPos
: The starting position in the destination array.length
: The number of array elements to be copied.
Throws:
IndexOutOfBoundsException
if copying would cause access of data outside array bounds.ArrayStoreException
if an element in thesrc
array could not be stored into thedest
array because of a type mismatch.NullPointerException
if eithersrc
ordest
isnull
.
Examples
Basic Usage
To demonstrate the basic usage of arraycopy()
, we will copy elements from one array to another.
Example
public class ArrayCopyExample {
public static void main(String[] args) {
int[] srcArray = {1, 2, 3, 4, 5};
int[] destArray = new int[5];
System.arraycopy(srcArray, 0, destArray, 0, srcArray.length);
System.out.println("Destination array: ");
for (int i : destArray) {
System.out.print(i + " ");
}
}
}
Output:
Destination array:
1 2 3 4 5
Handling Overlapping Arrays
The arraycopy()
method can handle overlapping source and destination arrays correctly.
Example
public class OverlappingArraysExample {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
System.arraycopy(array, 1, array, 2, 3);
System.out.println("Array after overlapping copy: ");
for (int i : array) {
System.out.print(i + " ");
}
}
}
Output:
Array after overlapping copy:
1 2 2 3 4
Copying Part of an Array
You can also copy only a part of an array to another array.
Example
public class PartialArrayCopyExample {
public static void main(String[] args) {
int[] srcArray = {1, 2, 3, 4, 5};
int[] destArray = new int[3];
System.arraycopy(srcArray, 1, destArray, 0, 3);
System.out.println("Partial destination array: ");
for (int i : destArray) {
System.out.print(i + " ");
}
}
}
Output:
Partial destination array:
2 3 4
Error Handling
It's important to handle potential exceptions that arraycopy()
might throw.
Example
public class ErrorHandlingExample {
public static void main(String[] args) {
try {
int[] srcArray = {1, 2, 3, 4, 5};
int[] destArray = new int[5];
// Intentionally causing an IndexOutOfBoundsException
System.arraycopy(srcArray, 0, destArray, 0, 10);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: Attempted to copy elements out of array bounds.");
} catch (ArrayStoreException e) {
System.out.println("Error: Type mismatch between source and destination arrays.");
} catch (NullPointerException e) {
System.out.println("Error: Source or destination array is null.");
}
}
}
Output:
Error: Attempted to copy elements out of array bounds.
Real-World Use Case
Resizing an Array
In a real-world scenario, you might need to resize an array dynamically. The arraycopy()
method can be used to copy elements from the old array to the new larger array.
Example
import java.util.Arrays;
public class ResizeArrayExample {
public static void main(String[] args) {
int[] originalArray = {1, 2, 3};
int[] newArray = new int[5];
System.arraycopy(originalArray, 0, newArray, 0, originalArray.length);
System.out.println("New resized array: " + Arrays.toString(newArray));
}
}
Output:
New resized array: [1, 2, 3, 0, 0]
Conclusion
The System.arraycopy()
method in Java provides a fast and efficient way to copy elements from one array to another. By understanding how to use this method, you can perform various array manipulations, handle overlapping arrays, copy parts of arrays, and handle potential errors. Whether you are resizing arrays, merging arrays, or shifting elements, the arraycopy()
method offers used for working with arrays in Java.
Comments
Post a Comment
Leave Comment