Java 在这段代码中,数组在哪里超出了边界?
我是Java新手,我遇到了这个错误,我可以设法理解它是从哪里来的。我在做FFT分析,我得到一个错误:Java 在这段代码中,数组在哪里超出了边界?,java,arrays,indexoutofboundsexception,lang,Java,Arrays,Indexoutofboundsexception,Lang,我是Java新手,我遇到了这个错误,我可以设法理解它是从哪里来的。我在做FFT分析,我得到一个错误: java.lang.ArrayIndexOutOfBoundsException: 66, at DrawingFunction.main(DrawingFunction.java:66) at AnimationGen.begin(AnimationGen.java:54) at FFTclass.main2(FFTclass.java:213) at fftTry.main(fftTry.j
java.lang.ArrayIndexOutOfBoundsException: 66,
at DrawingFunction.main(DrawingFunction.java:66)
at AnimationGen.begin(AnimationGen.java:54)
at FFTclass.main2(FFTclass.java:213)
at fftTry.main(fftTry.java:34)
fftTry的代码如下所示:
public static void main(String[] args) {
final String NEWLINE = "\n";
String inputFile = "C:/Users/USER/Downloads/DD.wav"; // Place the wav file in the top level directory, ie S:/input.wav
File fileLocation = new File(inputFile);
System.out.println("START");
try {
double[] complexNumber = readFully(fileLocation);
System.out.println(NEWLINE + "Read file");
double[] realPart = complexNumber;
double[] imagPart = new double[realPart.length];
System.out.println("Length = " + realPart.length);
FFTclass FFT = new FFTclass(1024);
FFT.main2(realPart,imagPart,1024);
//for (int i = 0; i < complexNumber.length; ++i) {
// System.out.println(complexNumber[i]);
//}
} catch (Exception ex) {
ex.printStackTrace();
}
}
static double[] readFully(File file) throws UnsupportedAudioFileException, IOException {
AudioInputStream in = AudioSystem.getAudioInputStream(file);
AudioFormat fmt = in.getFormat();
double sampleRate = fmt.getSampleRate();
double frameSize = fmt.getFrameSize();
double auidioDuration = fmt.getFrameRate();
double channels = fmt.getChannels();
System.out.println("File length = " + file.length() + " ");
System.out.println("Sample Rate = " + sampleRate + " per second");
System.out.println("Frame size = " + frameSize + " per second");
System.out.println("Frame rate = " + auidioDuration + " per second");
System.out.println("Channels = " + channels);
System.out.println("Length in time = " + (file.length()/frameSize * auidioDuration) + " seconds");
System.out.println("Length in time in minutes = " + (int)(file.length()/(frameSize* auidioDuration *60)) + " minutes and " + ( (file.length()/(frameSize* auidioDuration *60))- (int)(file.length()/(frameSize* auidioDuration *60)) )* 60 + " seconds");
byte[] bytes;
try {
if (fmt.getEncoding() != Encoding.PCM_SIGNED) {
throw new UnsupportedAudioFileException();
}
// read the data fully
bytes = new byte[in.available()];
in.read(bytes);
} finally {
in.close();
}
int bits = fmt.getSampleSizeInBits();
double max = Math.pow(2, bits - 1);
ByteBuffer bb = ByteBuffer.wrap(bytes);
bb.order(fmt.isBigEndian() ?
ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN);
double[] samples = new double[bytes.length * 8 / bits];
// convert sample-by-sample to a scale of
// -1.0 <= samples[i] < 1.0
for (int i = 0; i < samples.length; ++i) {
switch (bits) {
case 8:
samples[i] = (bb.get() / max);
break;
case 16:
samples[i] = (bb.getShort() / max);
break;
case 32:
samples[i] = (bb.getInt() / max);
break;
case 64:
samples[i] = (bb.getLong() / max);
break;
default:
throw new UnsupportedAudioFileException();
}
}
return samples;
}
publicstaticvoidmain(字符串[]args){
最后一个字符串换行符=“\n”;
String inputFile=“C:/Users/USER/Downloads/DD.wav”;//将wav文件放在顶级目录中,即S:/input.wav
文件文件位置=新文件(inputFile);
系统输出打印项次(“开始”);
试一试{
double[]complexNumber=readfull(文件位置);
System.out.println(换行符+读取文件);
double[]realPart=complexNumber;
double[]imagPart=新的double[realPart.length];
System.out.println(“Length=“+realPart.Length”);
FFTclass FFT=新的FFTclass(1024);
FFT.main2(realPart,imagPart,1024);
//对于(int i=0;i //-1.0我将在这里查看这些行:
double xPoint[][][] = new double[value][fps][64];
//...
for (int j = 0; j < (44100 / fps); j += ((44100 / fps) / 64)) {
xPoint[counter][i][j] = magnitude[counter][i][j];
double-xPoint[][]=new-double[value][fps][64];
//...
对于(整数j=0;j<(44100/fps);j+=((44100/fps)/64)){
xPoint[counter][i][j]=震级[counter][i][j];
请注意,输入的fps是30,因此((44100/30)/64))=22.96875(或作为int的22)。因此,当该循环迭代时,j将如下所示:
- j=0
- j=22
- j=44
- j=66(数组超出范围)
fps的值是30(如果我没有遗漏任何内容的话)。44100/fps=1470。因此,j从0变为1469。但是,数组的第三维度只有64
double xPoint[][][] = new double[value][fps][64];
double yPoint[][][] = new double[value][fps][64];
for (int counter = 0; counter < value; counter++) {
for (int i = 0; i < fps; i++) {
for (int j = 0; j < (44100 / fps); j += ((44100 / fps) / 64)) {
xPoint[counter][i][j] = magnitude[counter][i][j];
double-xPoint[][]=new-double[value][fps][64];
双Y点[][]]=新的双[值][fps][64];
用于(int计数器=0;计数器<值;计数器++){
对于(int i=0;i
你能准确地显示它是哪一行吗?我的猜测是j>=64
,但在不知道是哪一行的情况下,我无法告诉你导致错误的确切部分。堆栈跟踪会准确地告诉你问题出在哪里。@mustaccio如果你跟我说话,我太懒了,无法在这里数行。“在这段代码中,数组在哪里超出了边界?”在DrawingFunction.java
中的第66行,以哪一行为准(我们看不到行号)。
public int second = 0;
Timer t = new Timer();
TimerTask timeTask = new TimerTask() {
@Override
public void run() {
second++;
}
};
public double addingAngle = 0;
public int frameWidth = 1920;
public int frameHeigth = 1080;
public int value;
public int fps;
public double[][][] magnitude;
double xPoint[][][];
double yPoint[][][];
public void main(int value, int fps, double[][][] magnitude) {
this.value = value;
this.fps = fps;
this.magnitude = magnitude;
///////////////////////////////////////////////////////////////////////////////////////////////////
double xPoint[][][] = new double[value][fps][64];
double yPoint[][][] = new double[value][fps][64];
for (int counter = 0; counter < value; counter++) {
for (int i = 0; i < fps; i++) {
for (int j = 0; j < (44100 / fps); j += ((44100 / fps) / 64)) {
xPoint[counter][i][j] = magnitude[counter][i][j];
for (int t = 0; t < ((44100 / fps) / 65); t++) { //64 is the number of bands taken into and only 55 from them are used
if(j+t < magnitude[0][0].length) {
xPoint[counter][i][j] += magnitude[counter][i][j + t];
}
System.out.println("Here xPoint[" +i+"]["+j+"]" + (j + t));
}
xPoint[counter][i][j] /= (int) ((44100 / fps) / 64);
}
int t = 0;
for (double angle = addingAngle; angle < 2 * Math.PI + addingAngle; angle += 2 * Math.PI / 55) {
xPoint[counter][i][t] *= Math.cos(angle); //All X points [second][frame][band]
yPoint[counter][i][t] *= Math.sin(angle); //All Y points [second][frame][band]
t++;
}
addingAngle += Math.PI / 110; //110 = 55 points * 2 so rotation is slower
}
this.xPoint = xPoint;
this.yPoint = yPoint;
// Up to here we have 64 xPoints and 64 yPoints, but only the first 55 of each are multiplied by the angle
// jl.setIcon(new ImageIcon("/images/T.png"));
}
for (int i = 0; i < 55; i++) {
System.out.println("Point[" + i + "] is: " + xPoint[0][0][i]);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
JFrame window = new JFrame();
window.add(new DrawingFunction());
window.pack();
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setLocationRelativeTo(null);
window.setSize(frameWidth, frameHeigth);
window.setBackground(Color.blue);
window.setVisible(true);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.black);
g.fillRect(800, 500, 300, 300); //g.translate(frameWidth/2, frameHeigth/2);
Polygon p = new Polygon();
for (int i = 0; i < 55; i++)
p.addPoint(15 * (int) xPoint[0][0][i], 15 * (int) yPoint[0][0][i]);
g.setColor(Color.black);
g.drawPolygon(p);
g.fillRect(800, 800, 400, 400);
validate(); // So image appears even if screen is small
double xPoint[][][] = new double[value][fps][64];
//...
for (int j = 0; j < (44100 / fps); j += ((44100 / fps) / 64)) {
xPoint[counter][i][j] = magnitude[counter][i][j];
double xPoint[][][] = new double[value][fps][64];
double yPoint[][][] = new double[value][fps][64];
for (int counter = 0; counter < value; counter++) {
for (int i = 0; i < fps; i++) {
for (int j = 0; j < (44100 / fps); j += ((44100 / fps) / 64)) {
xPoint[counter][i][j] = magnitude[counter][i][j];