这可能是一个愚蠢的问题,但我在思考这个问题时遇到了困难。使用LinkedList实现下一个和上一个按钮
我写了一个方法,它使用LinkedList来移动通过加载的MIDI乐器。我想创建一个下一个和一个上一个按钮,以便每次单击遍历LinkedList的按钮。
如果我硬编码itr.next();
或itr.previous();
多次我可以通过LinkedList的
public void setInsturment(Synthesizer start,MidiChannel currentChannel[])
{
try
{
start.open();
Soundbank bank = start.getDefaultSoundbank();
start.loadAllInstruments(bank);
LinkedList<Instrument> currentInstrument = new LinkedList<Instrument>();
Instrument instrs[] = start.getLoadedInstruments();
currentInstrument.add(instrs[0]);
currentInstrument.add(instrs[1]);
currentInstrument.add(instrs[2]);
currentInstrument.add(instrs[3]);
currentInstrument.add(instrs[4]);
ListIterator itr = currentInstrument.listIterator();
itr.next();
itr.next();
itr.next();
// nextInstrument();
currentChannel[1].programChange(0,itr.nextIndex());
}
catch(MidiUnavailableException e)
{
System.out.print("error");
}
}
我有很多的麻烦让一个按钮,可以通过列表遍历遍历。有没有一种有效的方法来做到这一点?我尝试过这样的事情,但没有成功。
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == nextButton)
{
sound.nextInstrument();
}
public void nextInstrument()
{
itr.next();
}
在此先感谢你们!
为什么链表?一个ArrayList和记住当前索引似乎更容易(特别是因为你已经有你的仪器在一个数组中)。 – Thilo 2012-04-24 02:02:36
我需要使用数据结构,并且我熟悉链表,但是我不知道Java有ArrayLists。 – FireStorm 2012-04-24 02:10:36