java.lang.IndexOutOfBoundsException:索引:0,大小:0异常 [英] java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 exception

查看:127
本文介绍了java.lang.IndexOutOfBoundsException:索引:0,大小:0异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误java.lang.IndexOutOfBoundsException:索引:0,大小:0.

I am getting the error 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;
}

推荐答案

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

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

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

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

但是您没有在此之前在中等arraylist中的该位置添加任何元素.

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

因此,您需要先在索引0处添加元素,然后才可以使用set方法对其进行更新

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

这篇关于java.lang.IndexOutOfBoundsException:索引:0,大小:0异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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