有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中矩阵中的数组值分配

我想写一个基于矩阵的java代码。我用的是jama matrix

代码是这样的:

import Jama.Matrix;
public class Landmarkassist {
public static void main(String args[]){
    double [] time1st
 {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0};// Array of some 1st time slot.
    double[] time2nd={2.0,3.0,6.0,7.0,8.0};//Array of 2nd time slot
    double[] code={40.0,20.0,10.0,40.0,10.0};// Value of Marks
    Matrix Calculation=new Matrix(8,8);// A 8*8 Jama Matrix
    int k1=0,k2=0,l2=0,l1=0;
    while(k1<time1st.length && k2<time2nd.length){

        if(time1st[k1]<time2nd[k2]) {
       //Just to increment counter k1++
            k1++;
        }
   //If time slot 1 is equal to time slot 2 the we check for the codes if the codes matches with our criteria we update the cell of Jama matrix
        else if(time1st[k1]==time2nd[k2]){
            if(code[l1]==40.0){
                Calculation.set(l2,k1,40.0);//set the value to a specific cell of Jama Matrix l2 reperesent row number and k1 represent column no, and 40.0 is the value of that Cell
                l2++;
            }
            else if(code[l1]==20.0){
                Calculation.set(l2,k1,20.0);
                l2++;
            }
            else if(code[l1]==10.0){
                Calculation.set(l2,k1,10.0);
                l2++;
            }
            else{
                System.out.println("Nothing to do");
            }
            k2++;
            l1++;


        }



    }
    Calculation.print(9,1);
}

}

输出:

0.0   40.0    0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     20.0   0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  10.0 0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  40.0       0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0       10.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0  0.0        0.0

期望输出:

0.0   40.0    40.0    0.0   0.0  0.0  0.0        0.0
0.0   0.0     20.0   0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  10.0  10.0       0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0
0.0   0.0     0.0    0.0   0.0  0.0   0.0        0.0

这意味着所有相同数量的分数应该分配在同一行。所有40.0分都分配在第一行。所有10.0分都分配在第三排等

我如何迭代循环来实现这一点

提前谢谢


共 (0) 个答案