1

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);
} 
dryleaf
  • 415
  • 3
  • 18
Shaz
  • 1,443
  • 1
  • 27
  • 67

3 Answers3

1

You are shadowing your field

private ArrayList<ArrayList<Integer>> matrixOne;

with

ArrayList<ArrayList<ArrayList>> matrixOne = new ArrayList<ArrayList<ArrayList>>();

which doesnt have any type other than ArrayList. Try this :

matrixOne = new ArrayList<ArrayList<Integer>>();

for shadowing a class variable

Bedir Yilmaz
  • 3,823
  • 5
  • 34
  • 54
  • I see, but wouldn't i been making it a type ArrayList instead of Integer... as in: private ArrayList> matrixOne; matrixOne = new ArrayList>(); – Shaz Mar 30 '18 at 06:48
  • Yes it is a type but probably its type that you wont be able to cast to integer. If you want an ArrayList of ArrayLists of ArrayLists, you better at least make type of the last one clear. Which should be Integer. ArrayList>> matrixOne = new ArrayList>>(); – Bedir Yilmaz Mar 30 '18 at 06:52
  • So this is the confusing bit, do I need 3 levels of ArrayLists or just two, the question is regarding the approach and if there is a better way – Shaz Mar 30 '18 at 06:53
  • 1
    Well, when you say a matrix, 2D is what comes to my mind. Which is simply a list of lists. That means that you can use a list as following ArrayList> matrix = ArrayList>(); – Bedir Yilmaz Mar 30 '18 at 06:57
  • Okay, is the for looping the way to go to specify col and rows? ArrayList that is Matrix, has a row of ArrayList which has a Columns of ArrayList... in the code? – Shaz Mar 30 '18 at 07:04
  • I am not sure I understand your question correctly. But the idea here is to create rows first. You can add the rows to your main List one by one. After you're done, you will have the rows and columns you need. But to be honest, if you have a fixed size of columns and rows, use a two dimensional Array instead. – Bedir Yilmaz Mar 30 '18 at 07:08
  • It is a fixed sized rows/cols i.e. whatever is in the params of constructor. But can you give me an example of what a two dimensional array would look like, still not sure what the best solution is for this... this is confusing – Shaz Mar 30 '18 at 07:11
  • https://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array – Bedir Yilmaz Mar 30 '18 at 07:14
  • Ahh I see! If I wanted to retrieve the value from the multi ArrayList how would I do this with your approach... Im trying: return matrixOne.get(row).get(column) ... But im not sure how to reach the actually element – Shaz Mar 30 '18 at 07:23
  • 1
    **int a = matrix[i][j]** for _Two Dimensional Array_ and **Integer a = matrix.get(i).get(j)** for _ArrayList of ArrayLists_ – Bedir Yilmaz Mar 30 '18 at 07:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167857/discussion-between-3yanlis1bos-and-shaz). – Bedir Yilmaz Mar 30 '18 at 07:32
1

I would like to suggest you to use the dimensional array instead. Following is the simple implementation for transforming list(vector) to dimensional array. Inspired by R 's matrix(vec,nrow = 3,ncol = 3)

public static void main(String[] args){
    int[] vec = {2,3,4,5,6,7,8,9,10};
    toMatrix(vec,3,3);//parameters: vector(list),row of expected matrix,column of expected matrix
}
public static int[][] toMatrix(int[] vec,int row ,int col){
     int[][] matrix = new int[row][col]; 
        int vecIndex = 0;//list index to pop the data out from vector
        //Vector to matrix transformation
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(vecIndex==vec.length) break;
                matrix[i][j] = vec[vecIndex++];//pop the vector value
            }
        }

        // Displaying the matrix, can ignore if not necessary
        for(int i=0;i<row;i++){ 
            for(int j=0;j<col;j++){              
                System.out.print(matrix[i][j] + "   ");
            }
            System.out.println();
         }
        return matrix;
}
zawhtut
  • 8,335
  • 5
  • 52
  • 76
0

Try this:

public void insert(int row, int column, int value) {
    matrixOne.get(row).add(column, value);
} 
Pavlo Plynko
  • 586
  • 9
  • 27