使用LinkedList实现下一个和上一个按钮 [英] implementing next and previous buttons using a LinkedList

查看:96
本文介绍了使用LinkedList实现下一个和上一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我很难考虑清楚。

This might be a stupid question, but I'm having trouble thinking this through.

我编写了一种方法,该方法使用LinkedList在加载的MIDI乐器之间移动。我要创建一个下一个和上一个按钮,以便每次单击该按钮时都遍历LinkedList。

I wrote a method that uses a LinkedList to move through loaded MIDI instruments. I want to make a next and a previous button so that every time you click the button you traverse through the LinkedList.

如果我对 itr进行硬编码。 next(); itr.previous(); 可以遍历LinkedList多次

If I hardcode itr.next(); or itr.previous(); multiple times I can traverse through the 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");
    }

}

我有很多制作一个可以遍历列表的按钮时遇到了麻烦。有一种有效的方法可以做到这一点吗?我尝试了类似的尝试,但没有成功。

I'm having a lot of trouble making a button that can traverse through the list. Is there an efficient way to do this? I tried something like this with no success.

public void actionPerformed(ActionEvent e)
{
    if (e.getSource() == nextButton)
    {
        sound.nextInstrument();
    }

public void nextInstrument()
{
    itr.next();
}

谢谢大家!

推荐答案

好吧,嗯,一个链表是一个List,它的项可以通过索引访问,这不是按索引访问项的最佳结构,但是我真的不知道知道是否可以在该集合上使用游标,但可以将当前索引存储在实例变量上。

Well, hmm, a linked list is a List and its items can be accessed by index, this is not the optimal structure to access items by index but I really don't know if you can have a cursor on that kind of collection but you can store the current index on an instance variable.

如果您确实想要随机访问,则应考虑使用数组列表而不是链接列表。

If you really want random access, then you should consider using ArrayList instead of linked list.

示例:

class NextPrevList {
    private int index = 0;
    private List currentList; //initialize it with some list

    public Object next() {
        return list.get(++index);
    }
    public Object prev() {
        //maybe add a check for out of bounds
        if (index == 0) return null;
        return list.get(--index);
    }
}

我个人认为使用ArrayList会更出色而不是LinkedList

Personally I think it would be more performant with an ArrayList rather than a LinkedList

这篇关于使用LinkedList实现下一个和上一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆