【问题标题】:splitting the list in two parts in java在java中将列表分成两部分
【发布时间】:2014-04-04 05:10:24
【问题描述】:

我有一个在 for 循环内的数组,它首先被转换为一个列表,然后分成两半,列表的第一部分存储在 s1 列表中,第二部分存储在 w1 中,这个将递归完成,直到循环结束并且在方法结束时我将返回 s1 和 w1 这是我到目前为止所做的代码-:

   public Pair daubTrans( double s[] ) throws Exception
    {
     final int N = s.length;
      int n;
    //double t1[] = new double[100000];
     //List<Double> t1 = new ArrayList<Double>();
    // double s1[] = new double[100000];
    List<double[]> w1 = new ArrayList<double[]>();
  List<double[]> s1 = new ArrayList<double[]>();
 List<double[]> lList = new ArrayList<double[]>();
  //List<double[]> t1 = new ArrayList<double[]>();

  for (n = N; n >= 4; n >>= 1) {
      double[] t1=  transform( s, n );
      int length = t1.length;
     // System.out.println(n);
     // LinkedList<double> t1 =new LinkedList<double>( Arrays.asList(t1));

     /* for(double[] d: t1)
      {
          t1.add(d);
      }*/

      lList = Arrays.asList(t1);
      length=lList.size();
      //System.out.print(lList.size());

     // System.arraycopy(src, srcPos, dest, destPos, length)
/*   s1= t1.subList(0, 1);
     w1= t1.subList(0, 1); */
   /*    if(n==N)
     {
     s1= lList.subList(0, length/2-1);
     w1= lList.subList(length/2-1, length);
     }
     else
     {
     s1=lList.subList(( length/2), length);
      w1=lList.subList(( length/2), length);
     } */

//    System.arraycopy(t1,0, s1, n==N?0:t1.size()/2-1, t1.size()/2-1);
 // System.arraycopy(t1,(length/2), w1, n==N?0:t1.size()/2-1, t1.size()/2-1);
 // System.out.println(w1.length);

  }
  return new Pair(s1, w1);

 }

其中定义了对类以返回 2 列表,并且 transform 返回一个 double 类型的数组,该数组存储在 t1 数组中。 现在我在将 t1 数组转换为列表类型以及如何将由 t1 的元素形成的列表分成两部分时遇到问题。转换代码是 -:

      protected  double[] transform( double a[], int n )
    {
       if (n >= 4) {
        int i, j;
       int half = n >> 1;

    double tmp[] = new double[n];

    i = 0;
        for (j = 0; j < n-3; j = j + 2) {
          tmp[i]      = a[j]*h0 + a[j+1]*h1 + a[j+2]*h2 + a[j+3]*h3;
          tmp[i+half] = a[j]*g0 + a[j+1]*g1 + a[j+2]*g2 + a[j+3]*g3;
       i++;
         }
    // System.out.println(i);

     tmp[i]      = a[n-2]*h0 + a[n-1]*h1 + a[0]*h2 + a[1]*h3;
     tmp[i+half] = a[n-2]*g0 + a[n-1]*g1 + a[0]*g2 + a[1]*g3;


     for (i = 0; i < n; i++) {
        a[i] = tmp[i];

     }



  }


return a;
   } // transform

这是整个代码-:

  import java.util.Arrays;
   import java.util.List;
    import java.util.*;
     import java.lang.Math.*;

  class daub {
   protected final double sqrt_3 = Math.sqrt( 3 );
   protected final double denom = 4 * Math.sqrt( 2 );
    //
    // forward transform scaling (smoothing) coefficients
    //
    protected final double h0 = (1 + sqrt_3)/denom;
      protected final double h1 = (3 + sqrt_3)/denom; 
           protected final double h2 = (3 - sqrt_3)/denom; 
              protected final double h3 = (1 - sqrt_3)/denom;
         //
    // forward transform wavelet coefficients
        //
            protected final double g0 =  h3;
           protected final double g1 = -h2;
               protected final double g2 =  h1;
                protected final double g3 = -h0;

          //
           // Inverse transform coefficients for smoothed values
           //
           protected final double Ih0 = h2;
             protected final double Ih1 = g2;  // h1
           protected final double Ih2 = h0;
            protected final double Ih3 = g0;  // h3
           //
          // Inverse transform for wavelet values
               //
            protected final double Ig0 = h3;
         protected final double Ig1 = g3;  // -h0
           protected final double Ig2 = h1;
           protected final double Ig3 = g1;  // -h2
             List<Double> doubleList = new ArrayList<Double>();
       /**
        <p>
           Forward wavelet transform.

       protected  double[] transform( double a[], int n )
     {
  if (n >= 4) {
     int i, j;
     int half = n >> 1;

 double tmp[] = new double[n];

 i = 0;
     for (j = 0; j < n-3; j = j + 2) {
        tmp[i]      = a[j]*h0 + a[j+1]*h1 + a[j+2]*h2 + a[j+3]*h3;
        tmp[i+half] = a[j]*g0 + a[j+1]*g1 + a[j+2]*g2 + a[j+3]*g3;
    i++;
     }
    // System.out.println(i);

     tmp[i]      = a[n-2]*h0 + a[n-1]*h1 + a[0]*h2 + a[1]*h3;
     tmp[i+half] = a[n-2]*g0 + a[n-1]*g1 + a[0]*g2 + a[1]*g3;


     for (i = 0; i < n; i++) {
        a[i] = tmp[i];

     }



  }


return a;

} // 变换

         protected void invTransform( double a[], int n )
     {
  if (n >= 4) {
int i, j;
int half = n >> 1;
int halfPls1 = half + 1;

double tmp[] = new double[n];

//      last smooth val  last coef.  first smooth  first coef
tmp[0] = a[half-1]*Ih0 + a[n-1]*Ih1 + a[0]*Ih2 + a[half]*Ih3;
tmp[1] = a[half-1]*Ig0 + a[n-1]*Ig1 + a[0]*Ig2 + a[half]*Ig3;
j = 2;
for (i = 0; i < half-1; i++) {
  //     smooth val     coef. val       smooth val    coef. val
  tmp[j++] = a[i]*Ih0 + a[i+half]*Ih1 + a[i+1]*Ih2 + a[i+halfPls1]*Ih3;
  tmp[j++] = a[i]*Ig0 + a[i+half]*Ig1 + a[i+1]*Ig2 + a[i+halfPls1]*Ig3;
}
for (i = 0; i < n; i++) {
  a[i] = tmp[i];
}
  }
      }


         /**
           Forward Daubechies D4 transform
          */
          public Pair daubTrans( double s[] ) throws Exception
       {
  final int N = s.length;
  int n;
  //double t1[] = new double[100000];
  //List<Double> t1 = new ArrayList<Double>();
 // double s1[] = new double[100000];
  List<double[]> w1 = new ArrayList<double[]>();
  List<double[]> s1 = new ArrayList<double[]>();
 List<double[]> lList = new ArrayList<double[]>();
  //List<double[]> t1 = new ArrayList<double[]>();

  for (n = N; n >= 4; n >>= 1) {
      double[] t1=  transform( s, n );
      int length = t1.length;
     // System.out.println(n);
     // LinkedList<double> t1 =new LinkedList<double>( Arrays.asList(t1));

     /* for(double[] d: t1)
      {
          t1.add(d);
      }*/

      lList = Arrays.asList(t1);
      length=lList.size();
      //System.out.print(lList.size());

     // System.arraycopy(src, srcPos, dest, destPos, length)
/*   s1= t1.subList(0, 1);
     w1= t1.subList(0, 1); */
 if(n==N)
     {
     s1= lList.subList(0, length/2-1);
     w1= lList.subList(length/2-1, length);
     }
     else
     {
     s1=lList.subList(( length/2), length);
      w1=lList.subList(( length/2), length);
     } 

//    System.arraycopy(t1,0, s1, n==N?0:t1.size()/2-1, t1.size()/2-1);
 // System.arraycopy(t1,(length/2), w1, n==N?0:t1.size()/2-1, t1.size()/2-1);
 // System.out.println(w1.length);

  }
  return new Pair(s1, w1);

}

        /**

【问题讨论】:

  • 我建议您使用带有 自动格式化 的 IDE。
  • 您可能应该提供更多关于“现在我遇到问题...”的详细信息
  • @guest 我在从数组类型转换为列表类型时遇到错误,我无法在 for 循环内将 lList 拆分为 2 个部分
  • @christian 我正在使用 Eclipse,但即使 dat 也没有给我正确的解决方案
  • @user3262269 它不会为您提供解决方案,但它会使您的代码更加清晰易读,因此更容易可视化正在发生的事情。这只是一个提示。

标签: java arrays list


【解决方案1】:
  1. 请在本题中添加transform(s,n)的代码。

  2. 为什么会这样? for (n = N; n &gt;= 4; n &gt;&gt;= 1) {}好像更简单:for (int n = N; n &gt;= 4; n--) {}

  3. 这太疯狂了:List&lt;double[]&gt; 如果您想使用双打列表,请使用:List&lt;double&gt;

  4. 您希望看到什么结果?

【讨论】:

  • 我使用了 n>>-1,因为我每次都必须传递数组的一半来转换(s,n)fxn;最后我想要 2 个列表 s1 和 w1其中 s1 由每次迭代中返回的列表 lList 的前半部分形成,并在 s1 中附加内容而不重写,而 w1 是由列表 lList 的后半部分组成
  • 嗯,我觉得你选择的选择方式有点复杂……为了进一步的代码分析,请把上面的代码清除一下好吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-01
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多