Retrieving a specific range of data from an array (Java)
I want to populate a ComboBox
with the default 128 MIDI instruments, but calling Synthesizer.getDefaultSoundbank().getInstruments()
returns a list of every instrument available (more than 400 on my machine).
I then copy the list of all the available instruments into an Object
array (named _instruments
), although it g开发者_如何学Goives me everything I could ever need, I only need the first 128 elements.
_soundbank = _synthesizer.getDefaultSoundbank();
_synthesizer.loadAllInstruments(_soundbank);
_synthesizer.close();
_instrument = _soundbank.getInstruments();
Is there a specific way to get the first set of instruments or would it be possible to simply trim anything after the first 128 elements in an array? That way I would only be left with the first full set.
I hope that makes sense, it's an awkward scenario. Thanks!
The shortest and more comfortable way to do it would be using Arrays.copyOf
. See below:
_instrument = Arrays.copyOf(_soundbank.getInstruments(), 128);
See the Javadoc for more info.
What about Java's own Arrays.copyOfRange(..) to extract the first 128?
精彩评论