desislava_topuzakova

00. Copy Matrix

Jan 15th, 2022
644
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package MultidimensionalArrays_02;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class CopyMatrix_demo {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int [][] old = new int[4][4];
  9.         fillMatrix(old, scanner);
  10.         int [][] copied = new int[2][4];
  11.  
  12.         for (int row = 0; row < 4; row++) {
  13.             for (int col = 0; col < 4; col++) {
  14.                 copied[row][col] = old[row][col];
  15.             }
  16.         }
  17.         printMatrix(copied);
  18.  
  19.     }
  20.  
  21.     private static void fillMatrix(int[][] matrix, Scanner scanner) {
  22.         for (int row = 0; row < matrix.length; row++) {
  23.             for (int col = 0; col < matrix.length; col++) {
  24.                 matrix[row][col] = scanner.nextInt();
  25.             }
  26.         }
  27.     }
  28.  
  29.     private static void printMatrix(int[][] matrix) {
  30.         for (int row = 0; row < 4; row++) {
  31.             for (int col = 0; col < 4; col++) {
  32.                 System.out.print(matrix[row][col] + " ");
  33.             }
  34.             System.out.println();
  35.         }
  36.     }
  37. }
  38.  
RAW Paste Data