当我将一个对象添加到我的arraylist时,它会用该那个对象重新复制所有对象

时间:2017-06-08 01:05:47

标签: java arraylist

public static ArrayList<Chord> createRisingChordList(double duration, double[] frequencies, int count){
        ArrayList<Chord> chordList = new ArrayList<Chord>();
        double multiplier = 1.224;


        for(int i = 0; i < count; i++){

            Chord chord = new Chord(duration, frequencies);
            chordList.add(chord);

            frequencies[0] *= multiplier;
            frequencies[1] *= multiplier;
            frequencies[2] *= multiplier;

        }


        for(int x = 0; x < count; x++){

            chordList.get(x).play();

        }

        return chordList;
    }

///

double[] frequencies = {110.0, 110.0*1.224, 110.0*1.224*1.224};
        ArrayList<Chord> risingChords = Final.createRisingChordList(0.2, frequencies, 10);
        for (Chord risingChord: risingChords) {
            StdOut.println("Playing: " + risingChord);
            risingChord.play();
        }

为什么我的输出是这样的:

Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]
Playing: [0.2:  830.2461363882288 1016.2212709391921 1243.8548356295712]

我可以确认它有正确的对象添加它只是因为某些原因替换它们。这是我添加每个对象后数组的输出。

The updated List after: 0 iterations


[0.2:  134.64 164.79935999999998 201.71441663999997]
The updated List after: 1 iterations


[0.2:  164.79935999999998 201.71441663999997 246.89844596735995]
[0.2:  164.79935999999998 201.71441663999997 246.89844596735995]
The updated List after: 2 iterations


[0.2:  201.71441663999997 246.89844596735995 302.2036978640486]
[0.2:  201.71441663999997 246.89844596735995 302.2036978640486]
[0.2:  201.71441663999997 246.89844596735995 302.2036978640486]
The updated List after: 3 iterations


[0.2:  246.89844596735995 302.2036978640486 369.8973261855955]
[0.2:  246.89844596735995 302.2036978640486 369.8973261855955]
[0.2:  246.89844596735995 302.2036978640486 369.8973261855955]
[0.2:  246.89844596735995 302.2036978640486 369.8973261855955]

Etc
etc
etc

根据我的理解,我的和弦课程中没有静态字段

和弦课程:

public class Chord {
     double duration, frequencies[];

    public Chord(double duration, double[] frequencies) {

        this.duration = duration;
        this.frequencies = frequencies;

    } 

    public void play(){

        playChord(duration, frequencies);

    }

    public String toString(){
        String freq = "";

        for(int i = 0; i < frequencies.length; i++){

            freq = freq + " " + frequencies[i];
        }

        return "[" + duration + ": " + freq + "]";
    }

    private void playChord(double duration, double[] frequencies) {
        final int sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);
        final double[] slices = new double[sliceCount+1];
        for (int i = 0; i <= sliceCount; i++) {
            double chord = 0.0;
            for (double frequency: frequencies) {
                chord += Math.sin(2 * Math.PI * i * frequency / StdAudio.SAMPLE_RATE);
            }
            slices[i] = chord/frequencies.length;
        }
        StdAudio.play(slices);
    }

}

0 个答案:

没有答案