[英]Java parameter passing int[][]
我试图用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 [] []?
如何提取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++) {
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.