-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUtility.java
More file actions
42 lines (35 loc) · 1.18 KB
/
Copy pathArrayUtility.java
File metadata and controls
42 lines (35 loc) · 1.18 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
import java.util.Scanner;
public class ArrayUtility {
public static int[] inputArray() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
return arr;
}
public static void displayArray(int[] arr) {
System.out.print("Array elements: ");
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
public static int[][] input2DArray() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter rows and columns: ");
int rows = scanner.nextInt();
int cols = scanner.nextInt();
int[][] arr = new int[rows][cols];
System.out.println("Enter " + (rows * cols) + " elements:");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = scanner.nextInt();
}
}
return arr;
}
}