2014-04-04 39 views
0

我有一個數組,它位於for循環內部,它首先被轉換爲一個列表,然後分成兩部分,列表的第一部分存儲在s1列表中,第二部分部分被存儲在W1,這是要直到循環結束,並在方法結束時,我將回到S1和W1是這樣的代碼,我已經這樣做了遠完成遞歸:在java中分割兩部分的列表

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); 

} 

其中pair類定義爲返回2列表,並且transform將返回存儲在t1數組中的double類型的數組。 現在我在將t1數組轉換爲列表類型以及如何將由t1元素形成的列表拆分爲2個部分時遇到問題。用於變換的代碼是 - :

 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); 

}

 /** 
+3

我建議你使用帶* auto-formatting *的IDE。 – Christian

+0

你應該提供更多的細節,「現在我遇到了問題......」 – guest

+0

@客戶我在從數組類型轉換爲列表類型時出錯,我無法將lList拆分成2個部分for循環 – user3262269

回答

0
  1. 請在此問題上添加transform(s,n)的代碼。

  2. 這是爲什麼? for (n = N; n >= 4; n >>= 1) {}這似乎是更容易:for (int n = N; n >= 4; n--) {}

  3. 這太瘋狂了:List<double[]>如果您想使用雙打的名單,然後使用此:List<double>

  4. 什麼是你想看到的結果?

+0

我已經使用n >> -1,因爲我必須每次傳遞數組的一半來變換(s,n)fxn;最後我想要有2個列表s1和w1,其中s1是從前半部分形成的的列表lList在每次迭代中返回的內容在s1中的內容而不是重寫,而w1是列表的後半部分列表lList – user3262269

+0

嗯,我想你選擇一點點複雜的選擇方法...對於進一步代碼分析,你能清除上面的代碼嗎? – mig8