Trying to create a matrix object of generic type that has int columns, rows, and values. Note: The code below uses integer type to simplify.
Example output:
21 703 22 23
3 3 13 13 6
Or
studone studtwo studthree
studfour studnine studten
studran studmoreran studplus
Attempt:
- Decided to create an ArrayList as it can be expanded in size
My idea: Matrix will have col, rows... so rows of x ArrayList, and cols of y ArrayList
- Not able to test the code but I feel there must be a better way, the for loop seems excessive?
Here is the constructor:
private ArrayList<ArrayList<Integer>> matrixOne;
public Matrix(int rows, int columns) {
this.rows = rows;
this.columns = columns;
matrixOne = new ArrayList<ArrayList<ArrayList>>();
for(int i = 0; i < rows; i++) {
matrixOne.add(new ArrayList<ArrayList>());
}
for(int j = 0; j < columns; j++) {
matrixOne.get(j).add(new ArrayList<Integer>());
}
}
ISSUE: When trying to add value to a particular row/col, I get the following error in below method: The method add(int) is undefined for the type Integer
// on method .add() <-------- error
public void insert(int row, int column, int value) {
matrixOne.get(row).get(column).add(value);
}