Initializes a Java object instance that contains an array of objects

The following code is correct:

public Sample mOboeSamples[] = { new Sample(1,1),new Sample(1,2) };
public Sample mGuitarSamples[] = { new Sample(1,2) };
public SampleSet mSampleSet[] = { 
        new SampleSet( "oboe",mOboeSamples ),new SampleSet( "guitar",mGuitarSamples)
        };

But I want to write something similar:

public SampleSet mSampleSet[] = { 
        new SampleSet( "oboe",{ new Sample(1,2) } ),2) } )
        };

This does not compile

Is there some grammar I lack, or is it a language 'function'?

Solution

You need to tell it the type of array you pass as a parameter:

public SampleSet mSampleSet[] = { 
    new SampleSet( "oboe",new Sample[] { new Sample(1,2) } )
};

Without new expressions, braces are syntactically invalid (because they are initializers - in this case - but you didn't say anything to initialize)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>