java program on array operations
Answers
Answer:
Example:
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)); }
Java – Array Operations
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:
C
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));
}
}
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]