class Matrix {
private int rows;
private int cols;
private int values[][];
public Matrix() {
}
public Matrix(int rows, int cols) {
this.rows = (rows > 0) ? rows : 1;
this.cols = (cols > 0) ? cols : 1;
values = new int[this.rows][this.cols];
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = (rows > 0) ? rows : 1;
values = new int[this.rows][this.cols];
}
public int getCols() {
return cols;
}
public void setCols(int cols) {
this.cols = (cols > 0) ? cols : 1;
values = new int[this.rows][this.cols];
}
public void setElement(int row, int col, int value) {
values[row][col] = value;
}
public String toString() {
String result = "";
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
result += values[row][col] + "\t";
}
result += "\n";
}
return result;
}
}
public class Exec {
public static void main(String args[]) {
Matrix m1 = new Matrix(3, 4);
m1.setElement(0, 0, 12);
System.out.print(m1);
}
}
[此贴子已经被作者于2010-12-12 08:28:00编辑过]