-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray2D1.java
More file actions
49 lines (37 loc) · 1.75 KB
/
Array2D1.java
File metadata and controls
49 lines (37 loc) · 1.75 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
39
40
41
42
43
44
45
46
47
48
49
package com.mycompany.mavenproject1;
import java.util.Scanner;
public class Array2D1 {
public static void main(String[] args) {
// Create a 2D array to store 10 values (assuming a 2x5 array)
int[][] array2D = new int[2][5];
try ( // Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in)) {
System.out.println("Enter 10 values for a 2D array (2x5):");
// Use nested loops to get input for each element in the 2D array
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 5; col++) {
System.out.print("Enter value for element at row " + row + " and column " + col + ": ");
// Read the input and store it in the 2D array
array2D[row][col] = scanner.nextInt();
}
}
// Close the Scanner to avoid resource leak
}
// Display the entered values row-wise
System.out.println("Entered values row-wise:");
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 5; col++) {
System.out.print(array2D[row][col] + " ");
}
System.out.println(); // Move to the next line after printing each row
}
// Display the entered values column-wise
System.out.println("Entered values column-wise:");
for (int col = 0; col < 5; col++) {
for (int row = 0; row < 2; row++) {
System.out.print(array2D[row][col] + " ");
}
System.out.println(); // Move to the next line after printing each column
}
}
}