简体   繁体   English

查找索引超出范围异常

[英]Finding index out of bounds exception

All was well with this program until I made some changes in my addMainMenu method. 在我对addMainMenu方法进行一些更改之前,此程序一切正常。 Now it seems as though there is an array index out of bounds somewhere. 现在好像某个地方的数组索引越界越好。 Eclipse is not leading me too it. Eclipse也不带领我。 Does anyone know why this code has an array index out of bounds exception. 有谁知道为什么这段代码有超出范围的数组索引异常。

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.LinkedList.checkElementIndex(Unknown Source)
    at java.util.LinkedList.get(Unknown Source)
    at Menu.main(Menu.java:58)

import java.util.LinkedList;
import java.util.List;

public class Menu {
    LinkedList <LinkedList> mainMenuItems = new LinkedList <LinkedList> ();

    public void  Menu(){}

    public boolean addMainMenuItem(String newItem, String existingItem, int position){
        LinkedList <String> subMenuItems = new LinkedList <String> ();
        for (int i = 0; i<= mainMenuItems.size(); i++){
            if (mainMenuItems.get(i).contains(existingItem)){
                subMenuItems.addLast(newItem);
                int existingIndex = mainMenuItems.indexOf(existingItem);
                if (position == 1){
                    mainMenuItems.add(existingIndex + 1, subMenuItems);
                    break;
                }
                if (position == -1){
                    mainMenuItems.add(existingIndex, subMenuItems);
                    break;
                }
                if (i == mainMenuItems.size()){
                    subMenuItems.addLast(newItem);
                    mainMenuItems.add(subMenuItems);
                    break;
                }
            }
        }
    }
    return true;
}

public boolean deleteMainMenuItem(String item){
    if (mainMenuItems.contains(mainMenuItems.indexOf(item))){
        mainMenuItems.remove(mainMenuItems.indexOf(item));
        return true;
    }
    else{
        return false;
    }
}

public static void main(String[] args){
    Menu b = new Menu();
    b.addMainMenuItem("h", "b", 1);
    b.addMainMenuItem("hi", "h", 1);
    b.addMainMenuItem("i", "h", 1);
    System.out.println(b.mainMenuItems.get(0));
    b.deleteMainMenuItem("hi");
    System.out.println(b.mainMenuItems.get(0));
    System.out.println(b.deleteMainMenuItem("hi"));
}

There are two possible issues 有两个可能的问题

1 . 1 In this line 在这条线

for (int i = 0; i<= mainMenuItems.size(); i++)

you should have use i < mainMenuItems.size() 您应该使用i < mainMenuItems.size()

2 . 2 when you have not assigned any value to your LinkedList, you try to access an index of your LinkedList 当您尚未为LinkedList分配任何值时,您尝试访问LinkedList的索引

Change the <= to < in: i<= mainMenuItems.size() <=更改为< in: i<= mainMenuItems.size()

EDIT: 编辑:
If mainMenuItems is still empty, the line mainMenuItems.get(i).contains(existingItem) will generate java.lang.IndexOutOfBoundsException because mainMenuItems.get(i) doesn't exist. 如果mainMenuItems仍然为空,则行mainMenuItems.get(i).contains(existingItem)将生成java.lang.IndexOutOfBoundsException因为mainMenuItems.get(i)不存在。

First - Change the for loop as shown below 首先-如下所示更改for循环

for (int i = 0; i < mainMenuItems.size(); i++)

Also, you need to restructure addMainMenuItem method as shown below - 另外,您需要重构addMainMenuItem方法,如下所示-

for (int i = 0; i < mainMenuItems.size(); i++){
    if (mainMenuItems.size() == 0) {
        //you should put list initialization code here
        //means, logic when there is nothing in the mainMenuItems list
    } else {
        //when the list is not empty
    }
}

You are getting IndexOutOfBoundsException because you are doing get(index) on mainMenuItems - which is empty in your for loop. 您正在获取IndexOutOfBoundsException,因为您正在对mainMenuItems执行get(index)-在for循环中为空。

For example - 例如 -

List<String> list = new LinkedList<String>();
list.get(0);    //will throw the same exception

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM