我想使用for循环实现矩阵。为了创建矩阵,我使用了Jama Matrix Package。
这是我的代码
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]) {
Matrix Mytest=new Matrix(5,5);
for(int i=0; i<4; i++) {
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
这是我的输出:
1.000000 0.000000 0.000000 0.000000 0.000000
1.000000 1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 1.000000 0.000000 0.000000
0.000000 0.000000 1.000000 1.000000 0.000000
0.000000 0.000000 0.000000 1.000000 0.000000
没有编译错误或运行时错误。困难在于如何将(0,0)像元值设为2?由于此矩阵使用for循环构造,因此所有值都是对称构造的。那我怎么只能制作一个具有不同值的单元格呢?
期望输出:
2.000000 0.000000 0.000000 0.000000 0.000000
1.000000 1.000000 0.000000 0.000000 0.000000
0.000000 1.000000 1.000000 0.000000 0.000000
0.000000 0.000000 1.000000 1.000000 0.000000
0.000000 0.000000 0.000000 1.000000 0.000000
答案 0 :(得分:1)
我以前从未使用过Jama,但是从Javadoc来看,我认为您可以这样做:
func loadMenu(category: String, completion:@escaping([Recipe]?)->Swift.Void)
{
completion(recipe)
}
loadMenu("your category",completion:{(recipe:[Recipe]?) in
DispatchQueue.main.async {
recipeofyourtableview = recipe
self.yourTableView.reloadData()
}
})
答案 1 :(得分:1)
您可以在for循环内使用if条件,以使特定单元格具有不同的值。
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0;i<4;i++){
if(i == 0){
Mytest.set(i,i,2);
}
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
答案 2 :(得分:0)
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0;i<4;i++){
if (i==0) {
Mytest.set(i,i,2);
} else {
Mytest.set(i,i,1);
}
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
答案 3 :(得分:0)
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
// first column
Mytest.set(0,0,2);
Mytest.set(1,0,1);
// others columns
for(int i=1; i<4; i++){
Mytest.set(i,i,1);
Mytest.set(i+1,i,1);
}
Mytest.print(9,6);
}
}
或
import Jama.Matrix;
public class Matrixnonsym {
public static void main(String args[]){
Matrix Mytest=new Matrix(5,5);
for(int i=0; i<4; i++){
Mytest.set(i, i, i == 0 ? 2 : 1);
Mytest.set(i+1, i, 1);
}
Mytest.print(9,6);
}
}