Search code examples
javaarraysint

GetValueAt(Int a, int b) which returns int


I have map which is array[][], and I have a method

public int[][] getMap(){
   int [][] array = new int[this.size][this.size];
        for(int i=0;i<this.map.length;i++)
        {
            for(int j=0;j<this.map[i].length;j++)
            {
                array[i][j]=map[j][i];
            }
        }
        return array;
}

now I need to rewrite code which is above using just

public int getValueAt(int a, int b){}

Solution

  • I don't understand much what your are trying to achieve, but I think your want to do this:

    public int getValueAt(int a, int b){
        return this.map[a][b]
    }
    

    Then you could use the method as:

    public int[][] getTransformedCopy() {
        int[][] array = new int[this.map.length][this.map[0].length]
        for(int i = 0; i < this.map.length; i++) {
            for(int j = 0; j < this.map[0].length; j++) {
                  array[i][j] = getValueAt(j, i);
            }
        }
        return array;
    }