我正在尝试更新2D Vector中的元素(事实上我正在创建一个((n * n)xn)矩阵)基于我将在示例代码中提到的条件。
以下是示例代码。
public class TestMatrix{
private Vector<Vector<Integer>> matrix = new Vector<Vector<Integer>>();
public void matrixUpdate(){
int n = 9;
Vector<Integer> tmpMatrix = new Vector<Integer>();
for(int i = 0;i < n;i++) tmpMatrix.addElement(-1); // initialize
//tmpMatrix
for(int j = 0; j < n*n; j++){
marix.addElement(tmpMatrix);
} // initialize matrix
for(int i = 0; i < n*n; i++){
for(int j = 0; j < n; j++){
int a = i/n;
int b = i%n;
int c = j;
System.out.print("r" + a + "r" + b + "r" + c + "\t");
//condition for updating elements
if(a == b){
matrix.get(i).set(j,-1);
continue;
}
if(b == c){
matrix.get(i).set(j,-1);
continue;
}
if(a == c) {
matrix.get(i).set(j,-1);
continue;
}
matrix.get(i).set(j,99);
}
System.out.println(matrix.get(i));
}
for(int i = 0;i < (n*n); i++){
System.out.println(matrix.get(i));
}
}
public static void main(String[] args){
TestMatrix testMatrix = new TestMatrix();
testMatrix.matrixUpdate();
}
}
您可以运行上述代码并自行检查。按照我得到的输出并不是我所期待的。
任何帮助都是值得的。更好地实现上述逻辑非常值得注意。
干杯。
编辑:预期结果(对于n = 8个n * n大小的向量,每个元素作为n大小向量):
[
[99 -1 -1 -1 -1 -1 -1 -1],
[99 99 -1 -1 -1 -1 -1 -1],
[99 -1 99 -1 -1 -1 -1 -1],
[99 -1 -1 99 -1 -1 -1 -1],
[99 -1 -1 -1 99 -1 -1 -1],
[99 -1 -1 -1 -1 99 -1 -1],
[99 -1 -1 -1 -1 -1 99 -1],
[99 -1 -1 -1 -1 -1 -1 99],
[99 99 -1 -1 -1 -1 -1 -1],
[-1 99 -1 -1 -1 -1 -1 -1],
... total 64 lines ...
]