Java参数传递INT [] []
-
26-09-2019 - |
题
我试图用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
你的方式。我想看看f
本身施加DCT时的尺寸。
也就是说,我会改变
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++) {
// ...
不隶属于 StackOverflow