java.lang.IndexOutOfBoundsException:索引:0,大小:0 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39077303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:51:23  来源:igfitidea点击:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 exception

javaindexoutofboundsexception

提问by Manju

I am getting the error java.lang.IndexOutOfBoundsException: Index: 0, Size: 0.

我收到错误 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0.

public Collection<AdDistribution> getAdDistribution(byte srch, byte cont) throws IndexOutOfBoundsException {

    List<AdDistribution> mediums = new ArrayList<>();
    List<AdDistribution> adDistribution = new ArrayList<>();
            adDistribution.add(AdDistribution.SEARCH);
            adDistribution.add(AdDistribution.CONTENT);
            if (adDistribution.isEmpty()) {
                return null;
              }

    if (srch == 0 && cont == 0) {
        mediums = new ArrayList<>();
        mediums.set(0, adDistribution.get(0));
    }
    if (srch == 1 || cont == 1) {
        mediums = new ArrayList<>();
        if (srch == 1) {
            mediums.set(0, adDistribution.get(0));
        } else if (cont == 1) {
            mediums.set(0, adDistribution.get(1));
        }
    }
    if (srch == 1 && cont == 1) {
        mediums = new ArrayList<>();
        mediums.set(0, adDistribution.get(0));
        mediums.set(1, adDistribution.get(1));
    }
            return mediums;
}

回答by Andy Turner

You need to use

你需要使用

mediums.add(adDistribution.get(0));

rather than mediums.set(0, adDistribution.get(0));etc.

而不是mediums.set(0, adDistribution.get(0));等。

ArrayList.set(int, Object)requires there to be an element to replace:

ArrayList.set(int, Object)需要有一个元素来替换:

Replacesthe element at the specified position in this list with the specified element.

Throws: IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= size())

用指定的元素替换此列表中指定位置的元素。

抛出: IndexOutOfBoundsException- 如果索引超出范围 (index < 0 || index >= size())

In a new list, size() == 0, so set(0, something)fails.

在新列表中,size() == 0,所以set(0, something)失败。

回答by Devansh Kumar

The problem is that you are using set method to update element at index 0

问题是您正在使用 set 方法更新索引 0 处的元素

set(index,value) method needs an element to present at that index

set(index,value) 方法需要在该索引处显示一个元素

but you haven't added any element at that position in medium arraylist before that .

但是在此之前您还没有在 medium arraylist 的那个位置添加任何元素。

So you need to first add an element at index 0 thereafter only you can update it with set method

因此,您需要先在索引 0 处添加一个元素,然后才能使用 set 方法更新它

回答by Pohkalopokh

EDIT: I just removed the unnecessary if statements and simplified them to make your code more readable.
Then I have replaced the setfunctions with the addfunction, because the set only can set existing elements.

编辑:我只是删除了不必要的 if 语句并简化了它们以使您的代码更具可读性。
然后我用add函数替换了set函数,因为 set 只能设置现有元素。

EDIT2: Based on Andy Turner's answer, I modified my code. Thank you for the suggestions!

EDIT2:根据安迪特纳的回答,我修改了我的代码。感谢你的建议!

After these modifications, the code should look like this (add/remove/modify it for your needs):

经过这些修改后,代码应如下所示(根据您的需要添加/删除/修改它):

public Collection<AdDistribution> getAdDistribution(byte srch, byte cont) throws IndexOutOfBoundsException {

    List<AdDistribution> mediums = new ArrayList<>();

    if( ( srch == 0 && cont == 0 ) || srch == 1 ) {
        mediums.add(AdDistribution.SEARCH);
    }

    if( cont == 1 ) {
        mediums.add(AdDistribution.CONTENT);
    }

    return mediums;
}