Java参数传递int[][]

发布于 2024-10-03 23:41:30 字数 1518 浏览 5 评论 0原文

我正在尝试用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[][]作为私有变量并在当前类的构造函数中初始化?

I am trying to write a simple DCT algorithm in java. I want my findDCT method to have as a parameter an integer array like this:

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

Now, how would I declare this method and to be able to pass 'int[][] f' as a parameter instead of using f[][] declared as a private variable and initialized in the constructor of the current class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2024-10-10 23:41:30

如何提取 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++) {
              // ...

How about extracting the initializeCoefficients and changing the constructor from

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

to

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

You could then use the class like

double[][] dctApplied = new DCT(yourTwoDimF).applyDCT();

Also, I wouldn't use N the way you do. I would look at the dimensions of f itself when applying the DCT.

That is, I would change

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

to something like

    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++) {
              // ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文