Java参数传递int [] []

时间:2010-11-19 00:01:24

标签: java parameters parameter-passing table-valued-parameters dct

我正在尝试在java中编写一个简单的DCT算法。我希望我的findDCT方法将一个整数数组作为参数:

public class DCT {
    private Random generator = new Random();
    private static final int N = 8;
    private int[][] f = new int[N][N];
    private double[] c = new double[N];

    public DCT() {
        this.initializeCoefficients();
    }

    private void initializeCoefficients() {
        int value;

        // temporary - generation of random numbers between 0 and 255 
        for (int x=0;x<8;x++) {
            for (int y=0;y<8;y++) {
              value = generator.nextInt(255);
              f[x][y] = value;
              System.out.println("Storing: "+value+" in: f["+x+"]["+y+"]");
            }
        }

        for (int i=1;i<N;i++) {
            c[i]=1/Math.sqrt(2.0);
            System.out.println("Storing: "+c[i]+" in: c["+i+"]");
        }
        c[0]=1;
    }

    public double[][] applyDCT() {
        double[][] F = new double[N][N];
        for (int u=0;u<N;u++) {
              for (int v=0;v<N;v++) {
                double somme = 0.0;
                for (int i=0;i<N;i++) {
                  for (int j=0;j<N;j++) {
                    somme+=Math.cos(((2*i+1)/(2.0*N))*u*Math.PI)*Math.cos(((2*j+1)/(2.0*N))*v*Math.PI)*f[i][j];
                  }
                }
                somme*=(c[u]*c[v])/4;
                F[u][v]=somme;
              }
            }
        return F;
    }
}

现在,我将如何声明此方法并能够将'int [] [] f'作为参数传递,而不是将f [] []声明为私有变量并在当前类的构造函数中初始化?

1 个答案:

答案 0 :(得分:0)

如何提取initializeCoefficients并从

更改构造函数
public DCT() {
    this.initializeCoefficients();
}

public DCT(int[][] f) {
    this.f = f;
}

然后你可以使用像

这样的类
double[][] dctApplied = new DCT(yourTwoDimF).applyDCT();

另外,我不会像你那样使用N。在应用DCT时,我会查看f本身的维度。

也就是说,我会改变

    double[][] F = new double[N][N];
    for (int u=0;u<N;u++) {
          for (int v=0;v<N;v++) {
              // ...

类似

    double[][] F = new double[f.length][];
    for (int u = 0; u < f.length; u++) {
          F[u] = new double[f[u].length];
          for (int v=0;v<N;v++) {
              // ...