Heimdall
Heimdall

Reputation: 237

Java - Applying a vector method on a matrix row or column

I'm writing a method that does something to a vector (1-dimensional array). I want to apply it to rows and columns of a matrix (2-dimensional array: array of rows, each row is an array). I'd like to know the best approach to do so.

The vector method may take additional parameters. It could, for example, implement a sorting algorithm (mine doesn't, but it's complicated enough).

Now, if matrix is a matrix and I want to use the method on the 4th row, I simply plug matrix[3] into the method, so when the method accesses a[n], it will end up accessing matrix[3][n]. But it's not so simple to apply it onto a column, because whenever the method accesses a[n] I would want it to access matrix[n][7] (in case of 8th column).

I have in the past tried to apply a vector method to access list[index[n]] (list and index are vectors) whenever the method accessed a[n]. I used the copying way then (no 1. below).

Ways I've thought of (none elegant):

  1. Create a vector, copy the contents of a column into the vector, apply the method, then copy the contents of the vector back into the column of the matrix (and dispose of the vector / reuse it for application on another column later).

  2. Duplicate the method, one working with a[n], where a is a vector parameter, and one working with a[n][m], where a is a matrix parameter and m is an int parameter.

  3. Design the method so it either works with a vector or with a matrix column, but then the working of the method will be complicated by allowing for 2 cases each time an element is accessed.

  4. Maybe the parameter can be a function f (however implemented) so the method uses f(n)? Then for a vector vector, f given is such that f(n) returns (the reference to) vector[n]. For a row of matrix matrix, f(n) returns matrix[row][n] and for a column f(n) returns matrix[n][column]

Upvotes: 0

Views: 1293

Answers (1)

JB Nizet
JB Nizet

Reputation: 691943

Define an abstraction (assuming your matrix contains integers):

public interface IntVector {
    public int size();
    public int get(int index);
    public void set(int index, int value);
}

Change your algrithm so that it uses an IntVector rather than an array.

Then define two implementations:

public class RowVector implements IntVector {

    private final int[] row;

    public RowVector(int[] row) {
        this.row = row;
    }

    @Override
    public int size() {
        return row.length;
    }

    @Override
    public int get(int index) {
        return row[index];
    }

    @Override
    public void set(int index, int value) {
        row[index] = value;
    }
}

and

public class ColumnVector implements IntVector {
    private final int[][] matrix;
    private final int column;

    public ColumnVector(int[][] matrix, int column) {
        this.matrix = matrix;
        this.column = column;
    }

    @Override
    public int size() {
        return matrix.length;
    }

    @Override
    public int get(int index) {
        return matrix[index][this.column];
    }

    @Override
    public void set(int index, int value) {
        matrix[index][this.column] = value;
    }
}

Upvotes: 1

Related Questions