-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcopy_2Darray.java
40 lines (38 loc) · 1.38 KB
/
copy_2Darray.java
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
39
40
import java.util.Scanner;
public class copy_2Darray{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("enter dimensions: ");
int rows = sc.nextInt();
int cols = sc.nextInt();
int arr1[][] = new int[rows][cols]; // declare array 1
System.out.println("enter array : ");
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){ //input for array 1
arr1[i][j]=sc.nextInt();
}
}
System.out.println();
System.out.println("Original array");
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){ //print array 1
System.out.print(arr1[i][j]+" ");
}
System.out.println();
}
int arr2[][] = new int[rows][cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){ //copy array 1 and 2
arr2[i][j]=arr1[i][j];
}
}
System.out.println();
System.out.println("Copied Array");
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){ //print the copied array (array 2)
System.out.print(arr2[i][j]+" ");
}
System.out.println();
}
}
}