Advertisement
karlakmkj

Modifying Elements in a 2D Array

Aug 10th, 2021
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.Arrays;
  2. public class Modifying {
  3.     public static void main(String[] args) {
  4.         // Using the provided 2D array
  5.       int[][] intMatrix = {
  6.                 {1, 1, 1, 1, 1},
  7.                 {2, 4, 6, 8, 0},
  8.                 {9, 8, 7, 6, 5}
  9.         };  
  10.    
  11.         // Replace the number 4 in the 2D array with the number 0
  12.     intMatrix[1][1] = 0;
  13.     System.out.println(Arrays.deepToString(intMatrix));
  14.  
  15.         // Declare and initialize a new empty 2x2 integer 2D array called subMatrix
  16.     int[][] subMatrix = new int[2][2];
  17.  
  18.         // Using 4 lines of code, multiply each of the elements in the 2x2 top left corner of intMatrix by 5 and store the results in the subMatrix you created. Print subMatrix array.
  19.     subMatrix[0][0] = intMatrix [0][0] * 5;
  20.     subMatrix[0][1] = intMatrix [0][1] * 5;    
  21.     subMatrix[1][0] = intMatrix [1][0] * 5;
  22.     subMatrix[1][1] = intMatrix [1][1] * 5;
  23.     System.out.println(Arrays.deepToString(subMatrix));
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement