java,求救!利用随机函数生成矩阵;对角线求和;每行最小数及位置
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/18 21:29:18
java,求救!利用随机函数生成矩阵;对角线求和;每行最小数及位置
java,求救!利用随机函数生成矩阵;对角线求和;每行最小数及位置
java,求救!利用随机函数生成矩阵;对角线求和;每行最小数及位置
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Matrix {
private int i;
private int j;
private List rows;
public Matrix(int i,int j){
this.i = i;
this.j = j;
rows = new ArrayList();
for(int r = 0; r < i; r++){
List tempList = new ArrayList();
for(int c = 0; c < j; c++){
int temp = new Random().nextInt(100);
tempList.add(temp);
}
this.rows.add(tempList);
}
}
public void show(){
for(int r = 0; r < i; r++){
List row = (List)rows.get(r);
System.out.print('[');
int c = 0;
for(;c < j - 1; c++){
System.out.print( row.get(c) + "\t");
}
System.out.println(row.get(c) + "]");
}
}
public int diagonal(){
if(i == j){
int result = 0;
for(int r = 0; r < i; r++){
result += rows.get(r).get(r);
}
return result;
}else{
return -1;
}
}
public int getMin(int rr){
int r = rr - 1;
List row = rows.get(r);
int min = row.get(0);
for(int c = 0; c < j; c++){
if(row.get(c) < min){
min = row.get(c);
}
}
return min;
}
public String getPos(int rr){
int r = rr - 1;
return "" + rr + "," + (rows.get(r).indexOf(getMin(rr)) + 1);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
Matrix m = new Matrix(5,7);//5行7列
m.show();//显示矩阵
System.out.println(m.getMin(1));//第1行最小值
System.out.println(m.getPos(1));//第一行最小值位置
}
}
[55 60 63 15 74 56 50]
[6 56 69 64 46 28 71]
[92 43 83 25 79 82 8]
[60 62 24 87 46 98 16]
[84 44 38 95 15 23 78]
15
1,4