Java:如何让一个数组引用另一个数组的开头?
我试图做一个有趣的应用程序,将采取一个声音文件,修改它,并创建一个新的声音文件 我正在尝试做的一件事是使声音文件的长度增加一倍。所以我基本上做了一个新的数组,它是{2,2,3,3,7,7,8},而不是原来的{2,3,7,8}。我使用双打,这只是一个例子 我希望原始数组(示例)现在引用我刚刚创建的数组(temp)的开头,因此当文件保存时,现在保存temp数组 我可以增加或减少音乐文件的音量没有问题,并将其保存。我省略了代码的这一部分,因为它在这里不相关 如果有人愿意帮助我,我也想知道背后的原因 公共级音响{Java:如何让一个数组引用另一个数组的开头?,java,arrays,indexing,element,wav,Java,Arrays,Indexing,Element,Wav,我试图做一个有趣的应用程序,将采取一个声音文件,修改它,并创建一个新的声音文件 我正在尝试做的一件事是使声音文件的长度增加一倍。所以我基本上做了一个新的数组,它是{2,2,3,3,7,7,8},而不是原来的{2,3,7,8}。我使用双打,这只是一个例子 我希望原始数组(示例)现在引用我刚刚创建的数组(temp)的开头,因此当文件保存时,现在保存temp数组 我可以增加或减少音乐文件的音量没有问题,并将其保存。我省略了代码的这一部分,因为它在这里不相关 如果有人愿意帮助我,我也想知道背后的原因 公
double[] samples;
//So we only have to declare it once. Reference to an array
public Sound() {
//This constructor should initialize the samples array to be empty
samples = new double[0];
//Initialize an array with nothing because we will be using that to reference the
//location of other arrays
}
public void wavRead(String fileName) {
samples = WavIO.read(fileName);
//Samples was an adress of an array we set to 0. Then we used WavIO to create an aray of doubles, now
//we tell samples to reference this new address over here. Samples has the addsss of the new array
}
public void wavSave(String fileName) {
WavIO.write(fileName, samples);
}
public void lengthen() {
double[] temp = new double[(samples.length *2)];
int t = 0;
for(int i = 0; i < samples.length; i++) {
//Set a variable to increase the temp array by
temp[t] = samples[i];
//Have position 0 of temp = position 0 of soundRaw
t++;
//Increase the position in the temp array by one
temp[t] = samples[i];
//Have position 1 of temp array = position 0 of soundRaw
}
samples[0] = temp;
//Here is where I try and have the samples array reference the start of another array. I tried multiple things, this is simply the last effort I tried
}
}使用a,做你想做的事情要容易得多
要创建新的DoubleBuffer
,请执行以下操作。。。双重的然后,您将执行以下操作:
// Important!
origBuf.rewind();
final DoubleBuffer newBuf = DoubleBuffer.allocate(origBuf.remaining() * 2);
double value;
while (origBuf.hasRemaining()) {
value = origBuf.get();
newBuf.put(value).put(value);
}
然后newBuf.array()
还请注意,与任何XBuffer
一样,DoubleBuffer
,允许您设置endianness。替换此代码行
samples[0] = temp;
仅仅
samples = temp;
就够了!:) 只需使用samples=temp代码>而不是样本[0]=温度代码> 我早就试过了,下半场声音很安静。但是在重做你所说的之后,我意识到我再也没有增加过t,所以代码现在是我想要的。谢谢
samples = temp;