-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathArrayCopyExample.java
More file actions
executable file
·38 lines (29 loc) · 1.31 KB
/
ArrayCopyExample.java
File metadata and controls
executable file
·38 lines (29 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package arrays;
public class ArrayCopyExample {
public static void main(String[] args) {
// Source array
int[] sourceArray = {1, 2, 3, 4, 5};
// Destination array with the same length as source array
int[] destinationArray = new int[sourceArray.length];
// Copy elements from source array to destination array
for (int i = 0; i < sourceArray.length; i++) {
destinationArray[i] = sourceArray[i];
}
// Print the destination array to verify the copy
System.out.println("Destination array:");
for (int num : destinationArray) {
System.out.print(num + " ");
}
}
}
/*
This Java program demonstrates how to copy the contents of one array (sourceArray) into another array (destinationArray).
Source Array:
The sourceArray contains the original elements that we want to copy.
Destination Array:
The destinationArray is created with the same length as the sourceArray to hold the copied elements.
Copying Elements:
Using a for loop, each element from the sourceArray is copied to the corresponding index of the destinationArray.
Printing the Destination Array:
After copying, the destinationArray is printed to verify that the copy was successful.
*/