Java的数组列表导航 [英] Java arraylist navigation

查看:98
本文介绍了Java的数组列表导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我纠正我的if语句。我试图通过ArrayList的元素,但在某些情况下,下一个和previous按钮显示的命令行的错误进行导航。错误不同,但它们的共同点如下...

 异常螺纹AWT-EventQueue的-0java.lang.IndexOutOfBoundsException:索引....

继承人的code

 公共类buttonListener实现的ActionListener {
    公共无效的actionPerformed(ActionEvent的X){
        如果(x.getSource()== nxtButton){
            indexCounter ++;
            balanceFrame.setVisible(假);
            callDetail();            如果(indexCounter == filePaths.size() - 1){//最大计数......我们不希望超过最大数量,数组的大小
                indexCounter = -1;
        }
    }
    否则,如果(x.getSource()== prevButton){
        indexCounter--;
        balanceFrame.setVisible(假);
        callDetail();
        如果(indexCounter == 0){
            indexCounter = filePaths.size() - 1;
        }
    }
}}


解决方案

您获得的是例外,因为你是递增/递减计数器,访问ArrayList中,然后进行检查。

您需要做的是递增/递减计数器,做检查,然后访问该ArrayList

的ArrayList是具有基于位置0,所以你需要确保最小的位置可能是0,最大可能的位置比项目的ArrayList中的金额少的。

您需要做的是这样的:

  indexCounter ++;
如果(indexCounter>(filePaths.size - 1))
{
    indexCounter = filePaths.size - 1;
}
callDetail();

和在你的第二部分。

  indexCounter--;
如果(indexCounter℃,)
{
    indexCounter = 0;
}
callDetail();

这将导致用户保持观看的最后一个记录,如果他/她保持pressing旁边和第一条记录,如果他/她保持pressing previous。

在第二个音符,你似乎想实现一个循环列表,所以这应该工作:

  indexCounter ++;
如果(indexCounter>(filePaths.size - 1))
{
    indexCounter = 0;
}
callDetail();

和在你的第二部分。

  indexCounter--;
如果(indexCounter℃,)
{
    indexCounter = filePaths.size - 1;
}
callDetail();

Please help me correct my if statements. I am trying to navigate through the arraylist elements but in some cases next and previous button displays command line errors. the errors vary but they have in common the following...

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index....

heres the code

public class buttonListener implements ActionListener {
    public void actionPerformed(ActionEvent x){
        if (x.getSource() == nxtButton){
            indexCounter++;
            balanceFrame.setVisible(false);
            callDetail();

            if(indexCounter == filePaths.size()-1) { //max counter...we dont want to exceed the max number, size of array
                indexCounter = -1;
        }
    }
    else if (x.getSource() == prevButton){
        indexCounter--;
        balanceFrame.setVisible(false);
        callDetail();
        if(indexCounter == 0){
            indexCounter = filePaths.size()-1;
        }
    }
}}

解决方案

You are getting that exception because you are incrementing/decrementing the counter, access the ArrayList, and then, make the check.

What you need to do is to increment/decrement the counter, make the check, and then access the ArrayList.

ArrayLists are have 0 based locations, so you need to make sure that the smallest location possible is 0 and that the maximum location possible is one less than the amount of items in the ArrayList.

What you need to do is something like this:

indexCounter++;
if (indexCounter > (filePaths.size - 1))
{
    indexCounter = filePaths.size - 1;
}
callDetail();

and in your second part

indexCounter--;
if (indexCounter < 0)
{
    indexCounter = 0;
}
callDetail();

This will cause the user to keep viewing the last record if he/she keeps pressing next and the first record if he/she keeps pressing previous.

On second note, you seem to want to implement a Circular list, so this should work:

indexCounter++;
if (indexCounter > (filePaths.size - 1))
{
    indexCounter = 0;
}
callDetail();

and in your second part

indexCounter--;
if (indexCounter < 0)
{
    indexCounter = filePaths.size - 1;
}
callDetail();

这篇关于Java的数组列表导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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