-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.java
69 lines (57 loc) · 1.53 KB
/
Matrix.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Random;
/**
* User: EladB
* Description:
*/
public class Matrix {
public int matrix[][];
private int rows;
private int cols;
public Matrix(int rows, int cols, boolean isAutoRand) {
this.rows = rows;
this.cols = cols;
matrix = new int[rows][cols];
if (isAutoRand) {
generateMatrixWithRandomValues();
}
}
private void generateMatrixWithRandomValues() {
Random rand = new Random();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = rand.nextInt(10);
}
}
}
public int getCell(int row, int col) {
return matrix[row][col];
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public int[] getRow(int rowIndex) {
int[] res = new int[cols];
for (int j = 0; j < cols; j++) {
res[j] = matrix[rowIndex][j];
}
return res;
}
public int[] getCol(int colIndex) {
int[] res = new int[rows];
for (int j = 0; j < rows; j++) {
res[j] = matrix[j][colIndex];
}
return res;
}
public void printMatrix() {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
}