PREV NEXT
Copying an array:
1. Using arraycopy() method:
The arraycopy() method belongs to the System class. It is used to copy the contents of one array to the other.
Type
public static void |
Description |
Method
arraycopy(Object src, int srcPos, Object dest, int destPos, int length) |
Copies an array from the source array, beginning at the specified position, to the specified position of the destination array.
length – the number of array elements to be copied. |
Example:
1 2 3 4 5 6 7 8 |
public class ArrayCopying { public static void main(String[] args) { int Source[] = {5,6,7,8,9,10}; int Destination[] = new int[5]; System.arraycopy(Source, 1, Destination, 0, 5); System.out.print(Arrays.toString(Destination)); } } |
Output:
[6, 7, 8, 9, 10] |
2. Using copyOf() and copyOfRange() method:
The Arrays class has two commonly used methods for copying the content of array i.e. copyOf() and copyOfRange() method. The Generic type T[] can be replaced with any other primitive type.
Type
public static T[] public static T[] |
Description |
Method
copyOf(T[] original, int newLength) copyOfRange(T[] original, int from, int to) |
Returns a new array which is a copy of the array specified and padded with 0s to obtain the specified length. newLength is the number of array elements to be copied. Returns a new array containing the specified range from the original array, truncated or padded with nulls to obtain the required length. |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class ArrayCopying { public static void main(String[] args) { int Source[] = {5,6,7,8,9,10}; int Destination[] = java.util.Arrays.copyOf(Source, 8);//0 is padded for index beyond source System.out.println(Arrays.toString(Destination)); int Destination2[] = java.util.Arrays.copyOfRange(Source, 0, 5); System.out.println(Arrays.toString(Destination2)); } } |
Output:
[5, 6, 7, 8, 9, 10, 0, 0] [5, 6, 7, 8, 9] |
Manipulation of Array:
The java.util.Arrays class provides several methods for manipulating arrays (copying, sorting, searching etc.)
Commonly used methods of the class ‘Arrays’:
Methods | Description |
static int binarySearch(int[] a, int key) | Searches the specified array for the given value using the binary search algorithm and returns the value. |
static int binarySearch(int[] a, int fromIndex, int toIndex, int key) | Searches between the range specified in the specified array for the given value using the binary search algorithm and returns the value. |
static boolean equals(int[] a, int[] a2) | Returns true if the given arrays are equal. |
static void fill(int[] a, int val) | Assigns the value ‘val’ to each element in the array specified. |
static int hashCode(int[] a) | Returns a hashcode of the specified array. |
static void parallelSort(int[] a) | Sorts the specified array into ascending order. This works well with array with huge number of elements. |
static void sort(int[] a) | Sorts the specified array into ascending order. This is a serial sorting method and works well with small to large arrays. |