Created
April 26, 2023 08:43
-
-
Save R3DHULK/c70cf291219c7a131e71ef6f2153983c to your computer and use it in GitHub Desktop.
Matrix In Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Matrix { | |
private int[][] data; | |
private int rows; | |
private int cols; | |
public Matrix(int rows, int cols) { | |
this.rows = rows; | |
this.cols = cols; | |
this.data = new int[rows][cols]; | |
} | |
public int get(int row, int col) { | |
return this.data[row][col]; | |
} | |
public void set(int row, int col, int value) { | |
this.data[row][col] = value; | |
} | |
public int getRows() { | |
return this.rows; | |
} | |
public int getCols() { | |
return this.cols; | |
} | |
public void print() { | |
for (int i = 0; i < this.rows; i++) { | |
for (int j = 0; j < this.cols; j++) { | |
System.out.print(this.data[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
Matrix matrix = new Matrix(3, 3); | |
matrix.set(0, 0, 1); | |
matrix.set(0, 1, 2); | |
matrix.set(0, 2, 3); | |
matrix.set(1, 0, 4); | |
matrix.set(1, 1, 5); | |
matrix.set(1, 2, 6); | |
matrix.set(2, 0, 7); | |
matrix.set(2, 1, 8); | |
matrix.set(2, 2, 9); | |
matrix.print(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment