0

My code:

import org.ujmp.core.Matrix;
import org.ujmp.core.SparseMatrix;

public class part {
    public static void main(String args[]) throws Exception{
        Matrix Bigomega=Matrix.Factory.zeros(6,6);
        Matrix omega = SparseMatrix.Factory.zeros(6, 6);
        int []timea={1,2,3,4,5,6};
        int [] timeb={3};
        int k1=0,k2=0;
        while (k1 < timea.length && k2 < timeb.length ) {
            if (timea[k1] < timeb[k2]) {
                omega.setAsInt(1, k1, k1);
                omega.setAsInt(-1, k1, k1 + 1);
                omega.setAsInt(-1, k1 + 1, k1);
                omega.setAsInt(1, k1 + 1, k1 + 1);
                Bigomega = Bigomega.plus(omega);
                omega.clear();
                k1++;
            }
            else if (timea[k1] == timeb[k2]){
                omega.setAsInt(1, k1, k1);
                omega.setAsInt(-1, k1, k1 + 1);
                omega.setAsInt(-1,k1+1,k1);
                omega.setAsInt(1,k1+1,k1+1);
                Bigomega=Bigomega.plus(omega);
                omega.clear();
                k2++;
            }
        }
        System.out.println(Bigomega);
    }
}

Output:

1.0000    -1.0000     0.0000     0.0000     0.0000     0.0000
-1.0000     2.0000    -1.0000     0.0000     0.0000     0.0000
0.0000    -1.0000     2.0000    -1.0000     0.0000     0.0000
0.0000     0.0000    -1.0000     1.0000     0.0000     0.0000
0.0000     0.0000     0.0000     0.0000     0.0000     0.0000
0.0000     0.0000     0.0000     0.0000     0.0000     0.0000

The output that came that is not desired. The loop was terminated here after getting the timea[k1] == timeb[k2]. But I want after getting the similar element from timea and timeb the loop continue until all the elements of timea visit. As per my code it visits 1,2,3 from timea array and then discards. And this is very obvious because of while statement while (k1 < timea.length && k2 < timeb.length ). Because when it get loop counter k2++ k2 increment by 1. So after getting 3 from timeb k2 become 1.So when it get k2<timeb.length that means 1<1 which is false so the loop is terminated. But in that time {4,5,6} of timea is not visited. I want after executing else statement if statement executes again for {4,5,6}.

How can this be achieved?

Desired output:

1.0000    -1.0000     0.0000     0.0000     0.0000     0.0000
-1.0000     2.0000    -1.0000     0.0000     0.0000     0.0000
0.0000    -1.0000     2.0000    -1.0000     0.0000     0.0000
0.0000     0.0000    -1.0000     2.0000     -1.0000     0.0000
0.0000     0.0000     0.0000     -1.0000     2.0000     -1.0000
0.0000     0.0000     0.0000     0.0000     1.0000     -1.0000
4

1 回答 1

1

In your code is that in your if condition you do something if they are == and you do something if timea[k1] < timeb[k2] but you don't do anything if timea[k1] > timeb[k2]. That's why you miss some iterations. You can handle that case or make the else >= instead of just ==.

Another thing I would like to point out is that there are some coding conventions that are accepted in Java and you better use them to have a readable code. For example BigOmega should be called bigOmega with a lowercase.

And a last thing is your while loop. You can do it like that which will make it more readable than repeating code.

 while (k1 < timea.length && k2 < timeb.length ) {
        omega.setAsInt(1, k1, k1);
        omega.setAsInt(-1, k1, k1 + 1);
        omega.setAsInt(-1, k1 + 1, k1);
        omega.setAsInt(1, k1 + 1, k1 + 1);
        Bigomega = Bigomega.plus(omega);
        omega.clear();
    if (timea[k1] < timeb[k2])
        k1++;
     else          
        k2++;        
}
于 2018-08-02T13:08:59.967 回答