【发布时间】:2012-04-24 01:59:52
【问题描述】:
这可能是一个愚蠢的问题,但我很难考虑清楚。
我编写了一个使用 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 并记住当前索引似乎更容易(尤其是因为您已经将仪器放在一个数组中)。
-
我需要使用数据结构并且我熟悉链表,但是我不知道Java有ArrayLists。
标签: java swing linked-list jbutton actionlistener