首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

可以结束了。解决办法

2012-03-15 
可以结束了。z.B.double[]x_vektor{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}*window[0]{1,2,3,4,5,6}wind

可以结束了。
z.B.   double[]   x_vektor   =   {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
                    *   window[0]   ={1,2,3,4,5,6};           window.length   =   6;     每次拷贝6个元素
                    *   window[1]   ={5,6,7,8,9,10};           window.length   =   6;   第二次向右滑动4个元素后从第5个元素开始再次拷贝6个元素
                    *   window[2]   ={9,10,11,12,13,14};window.length   =   6;   再次滑动四个以此类推
                    *   window[3]   ={13,14,15};     window.length   =   5;
                    *  
                    *   public   static   void   arraycopy(Object   source,   int   srcIndex,  
                    *   Object   dest,   int   destIndex,     int   length)
                    *   public   static   double[]   subArray(double[]   orig,   int   off,   int   len)找到这个静态方法
        {
                               
                if   (off+len> orig.length)   throw   new   IllegalArgumentException( "requested   subarray   exceeds   array   length ");
                double[]   window   =   new   double[len];
                System.arraycopy(orig,   off,   window,   0,   len);
                return   window;
        }
                    */
[code=Java]


我想实现以下内容,不知道怎么弄啊

public   double[]   getWindow(){
                          double[]   x_vektor   =   getXVektor();//原数组,里面是若干个double数,长度肯定大于6个
                          double[]   window   =   new   double[windowLength];//   windowLength   =   6建立subarray长度为6,每次向后跨过4个元素
                         
                         
                          for(int   i   =   0;   (i   <   x_vektor.length)&&(i+4   <   x_vektor.length);   i   =   i+4   ){       //       这里只要i+4不越界就行                      
                                        for(int   j   =   0;   j   <windowLength     ;   j++){  
                                               


                                                while(i+j   <   x_vektor.length){                                                        
                                                      window[j]   =   Arrays.copyOfRange(x_vektor,   i+j   ,   windowLength   );//这个地方怎么弄啊
                                                }
                                          }System.out.print( " < < < < < < < < < < < < < < < < < < < < < < ");
                                       
                                  }
                         
                       
                          return   window;
                  }

[/code]

不知道如何将静态改为动态方法。


[code=Java]
public   double[]   getWindow(){
                          double   sum   =   0.0;
                          double[]   wh   =   getWh();
                          double[]   x_vektor   =   getXVektor();
                          double[]   y   =   new   double[windowLength];
                          double   window[]   =   new   double[windowLength];//   windowLength   =   6
                         
                          for(int   i   =   0;   i   <   x_vektor.length;   i=   i+2){
                                for(int   j   =   0;   j   <   windowLength;   j++){
                                        if(i+j   <   x_vektor.length){
                                                window[j]   =   x_vektor[i+j]   *   wh[j];      


                                                System.out.println( "print   window:   "   +   i   +   ":   "+window[j]);
                                                        sum   +=   window[j];
                                        }
                                        else{
                                                break;
                                        }
                                }
                                        System.out.println( "print   sum:   "   +   i   +   ":   "+sum);
                          }
                                System.out.println( "window.length:   "+   window.length);
                                System.out.println(window[0]);
                                System.out.println(window[2]);
                          return   window;
                  }
                   
                 
[/code]弄得差不多了才发现还是得变成arraylist,不然不能进行下面的计算,汗啊。。

[color=#FF0000]接下来是刚弄出来的,想返回array,但是不成功,咋弄啊[/color]

[code=Java]
  public   double[]   getWindow(){
                          double   sum   =   0.0;
                          double[]   wh   =   getWh();
                          double[]   x_vektor   =   getXVektor();
                         
                          ArrayList   doubleList   =   new   ArrayList();

                          double   window[]   =   new   double[windowLength];//   windowLength   =   6
                         
                          for(int   i   =   0;   i   <   x_vektor.length;   i=   i+2){


                                for(int   j   =   0;   j   <   windowLength;   j++){
                                        if(i+j   <   x_vektor.length){
                                                window[j]   =   x_vektor[i+j]   *   wh[j];      
                                                System.out.println( "print   window:   "   +   i   +   ":   "+window[j]);
                                                        sum   +=   window[j];                                                      
                                        }
                                        else{
                                                break;
                                        }
                                }    
                                        doubleList.add(sum);
                                        System.out.println( "print   doubleList(sum):   "   +   i   +   ":   "+doubleList);
                          }
                          double[]   y   =   new   double[doubleList.size()];
                          for(int   i   =   0;   i   <   doubleList.size();   i++){
                                  y[i]   =   doubleList.get(i);
//[color=#FF6600]这里的问题,把arraylist赋给array[/color]
                         
                          System.out.println(y[i]);}
                          return   y;
                  }


                   
                         
[/code]

搞定咯

[code=Java]
public   double[]   getWindow(){
                          double   sum   =   0.0;
                          double[]   wh   =   getWh();
                          double[]   x_vektor   =   getXVektor();
                         
                          ArrayList <Double>   doubleList   =   new   ArrayList <Double> ();

                          double   window[]   =   new   double[windowLength];//   windowLength   =   6
                         
                          for(int   i   =   0;   i   <   x_vektor.length;   i=   i+2){
                                for(int   j   =   0;   j   <   windowLength;   j++){
                                        if(i+j   <   x_vektor.length){
                                                window[j]   =   x_vektor[i+j]   *   wh[j];      
                                                System.out.println( "print   window:   "   +   i   +   ":   "+window[j]);
                                                        sum   +=   window[j];                                                      
                                        }
                                        else{
                                                break;
                                        }
                                }    
                                        doubleList.add(sum);


                                        System.out.println( "print   doubleList(sum):   "   +   i   +   ":   "+doubleList);
                          }
                          double[]   y   =   new   double[doubleList.size()];
                          for(int   i   =   0;   i   <   doubleList.size();   i++){
                                  y[i]   =   doubleList.get(   i   ).doubleValue();
                                 
                         
                          System.out.println(y[i]);}
                          return   y;
                  }
                   
[/code]

[解决办法]
window[j] += x_vektor[i+j];
[解决办法]
看了几遍才知道楼主想干啥,你这需要二维数组才能搞定,然而楼主你又没定义二维数组,修改如下:

Java code
    public double[][] getWindow() {        double[] x_vektor = getXVektor();        double[][] window = new double[x_vektor.length / 4 + 1][windowLength];        for (int i = 0; i < x_vektor.length; i = i + 4) {            for (int j = 0; j < windowLength; j++) {                if (i + j < x_vektor.length) {                    window[i][j] = x_vektor[i + j];                    System.out.println("print window: " + i + ": " + window[i][j]);                } else {                    break;                }            }        }        return window;    } 

热点排行