简体   繁体   English

Java中if else语句中的Jama矩阵和逻辑错误

[英]Jama matrix and Logical error in if else statement in java

I try to implement some code block.I have four array. 我尝试实现一些代码块。我有四个数组。

    double[]timea={1.0,2.0,3.0,4.0,5.0,6.0};
    double[]speed={11.0,12.0,8.0,13.0,9.0,6.0};
    double[]timeb={1.5,2.5,4.0,5.5};
    double[]speedb={12.3,8.5,6.9,7.8};

1st array define some time say time stamp and some relative speed corresponding each time block. 第一个数组定义一些时间,例如时间戳和每个时间块对应的一些相对速度。

Like in time 1.0 speed is 11.0,in time 2.0 speed is 12.0,in time 3.0 speed is 8.0.... etc. 像时间1.0速度是11.0,时间2.0速度是12.0,时间3.0速度是8.0 ....等等

3rd array define timestamp b with some corresponding speed. 第三数组定义具有相应速度的时间戳b。 Like at time 1.5 speed is 12.3, at time 2.5 speed is 3.8, at time 4.0 speed is 5.6 ...etc 就像时间1.5的速度是12.3,时间2.5的速度是3.8,时间4.0的速度是5.6 ...等等

I want to write down a program which merge those time and there speed with respect to time. 我想写一个程序,将那些时间和速度合并在一起。

So the desire output will be 所以欲望输出将是

1.0 11.0
1.5 12.3
2.0 12.0
2.5 8.5
3.0 8.0
4.0 13.0
4.0  6.9
5.0  9.0
5.5  7.8
6.0  6.0

I write down a code for that 我为此写下了代码

public class Check {
public static void main(String args[]){
    Matrix abc=new Matrix(10,2);
    double[]timea={1.0,2.0,3.0,4.0,5.0,6.0};
    double[]speed={11.0,12.0,8.0,13.0,9.0,6.0};
    double[]timeb={1.5,2.5,4.0,5.5};
    double[]speedb={12.3,8.5,6.9,7.8};
    int k=0,k1=0;
    while(k<timea.length){

            abc.set(k, 0, timea[k]);
            abc.set(k, 1, speed[k]);

        if(timea[k]<timeb[k1]){
            abc.set(k,0,timeb[k1]);
            abc.set(k,1,speedb[k1]);
            if(k1<timeb.length-1){
                k1++;
            }
        }
        else if(timea[k]>timeb[k1]){
            abc.set(k,0,timea[k]);
            abc.set(k,1,speed[k]);
        }
        k++;
    }
    abc.print(3,6);
}

} }

Program output: 程序输出:

1.500000 12.300000
2.500000 8.500000
4.000000 6.900000
5.500000 7.800000
5.500000 7.800000
6.000000 6.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000

So please help me to find out the logical error. 因此,请帮助我找出逻辑错误。

I think the easiest way to do this is just to dump everything into a single ArrayList and then just call sort(Comparator) to sort the data by time. 我认为最简单的方法是将所有内容转储到单个ArrayList ,然后调用sort(Comparator)按时间对数据进行排序。

After the data is sorted, add it to the matrix. 数据排序后,将其添加到矩阵。

The only real trick is to declare a class to hold the pairs of data so that they can be easily sorted. 唯一真正的诀窍是声明一个类来保存数据对,以便可以轻松地对它们进行排序。 After you have that it's literally one line of code to sort them. 获得这些内容后,实际上就是一行代码来对它们进行排序。

public class PairwiseSort {

   public static void main( String[] args ) {
      double[] timea = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
      double[] speed = {11.0, 12.0, 8.0, 13.0, 9.0, 6.0};
      double[] timeb = {1.5, 2.5, 4.0, 5.5};
      double[] speedb = {12.3, 8.5, 6.9, 7.8};

      ArrayList<Datum> data = new ArrayList<>();
      for( int i = 0; i < speed.length; i++ ) {
         data.add( new Datum( timea[i], speed[i] ) );
      }
      for( int i = 0; i < speedb.length; i++ ) {
         data.add( new Datum( timeb[i], speedb[i] ) );
      }

      data.sort( Comparator.comparing( Datum::getTime ) );
      System.out.println( data );

      Matrix abc=new Matrix(10,2);
      for( int i = 0; i < data.size(); i++ ) {
         Datum datum = data.get( i );
         abc.set( i, 0, datum.getTime() );
         abc.set( i, 1, datum.getSpeed() );

      }

   }
}

class Datum {

   double time;
   double speed;

   public Datum( double time, double speed ) {
      this.time = time;
      this.speed = speed;
   }

   public double getTime() {
      return time;
   }

   public void setTime( double time ) {
      this.time = time;
   }

   public double getSpeed() {
      return speed;
   }

   public void setSpeed( double speed ) {
      this.speed = speed;
   }

   @Override
   public String toString() {
      return "Datum{" + "time=" + time + ", speed=" + speed + '}';
   }

}

class Matrix {
   Matrix( int a, int b ) {}
   void set( int a, int b, double x ) {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM