Arraylist和ArrayIndexOutOfBoundsException之间的关系

时间:2012-09-21 15:13:34

标签: java

列表与数组之间的关系是什么?为什么我得到ArrayIndexOutOfBoundsException。?

  

基本上我将一个参数传递给一个方法,基于使用HQL,我返回一组 ArrayList 。现在我的问题是。当这个ArrayList返回超过0(大小)时,这正是我想要的方式。

     

但是当它返回0(大小)ArrayList时,我得到了Exception。这是为什么。任何机构都可以向我解释   

现在我再次怀疑,如果一个arraylist返回5(第一)& 0(我测试的尺寸第二)我没有得到任何例外。但是当它返回200个或更多元素时会产生异常。这是为什么? 是否有任何具体的常数。喜欢这么多元素数组不应该给IndexOutofBounds和类似的东西?任何机构可以向我解释一下吗?

这是我的代码:

  public void setUpDisplay()
  {
    if (_flatView || _expired || _issue){ // these are all my views.
        _deptBalances = fetchBalances(null);
    }
    else if (_searchGen.getGenericName() != null){ 
        _storeGenList.clear();
        _deptBalances = fetchBalances(_searchGen.getGenericName());
        if (!ListUtil.nullOrEmptyList(_deptBalances)){
            // i am displaying the result here.
        }
        else {
            displaying error message.
        }
    }
      // here i am using my _deptBalances to display(i am just putting this list into displaygroup).
} 

//这是我的方法。

    public List<DEPTStoreBalance> fetchBalances(String genName){
    EntityManager em // Creating instance to entity manager.
    List <DEPTStoreBalance> tempList = ListUtil.list();
    String expClause =  new String();
    if (_expired){
        expClause = "and gg.bSubjectToExpiration=true " +
                    "and msb.expirationDate <= :expDate ";
        if(_expiringOnly){
            expClause = expClause.concat(" and msb.expirationDate > :today");
        }
        else {
            expClause = expClause + 
                        "and (msb.expirationDate > :today " +
                        "or (balance.qtyBalance > 0 or balance.qtyInTransit > 0)) ";
        }
    }
    else {
        expClause = "and ((gg.bSubjectToExpiration=true " +
                    "and msb.expirationDate > :expDate) " +
                    "or gg.bSubjectToExpiration=false) ";

        if (_flatView || _issue){
            expClause = expClause.concat("and (balance.qtyBalance > 0 or balance.qtyInTransit > 0) ");
        }
        else if (genName != null){
            expClause = expClause.concat("and gg.genericName = :genName ");
        }
    }

    String hql = "select balance from DEPTStoreBalance as balance " +
                 " "+ // here are my joins with multiple tables.
                 "where dsg.store = :store " +
                 expClause;

    if (_issue)
        hql = hql.concat(" and dsi.deptIssue = :deptIssue");
    Query q = em.createQuery(hql);
    q.setParameter("store", _store); // here i am selecting the store(which is being changing in search component).
    if (_issue)//Only saleable items should be issued
        q.setParameter("expDate",12 months);
    else 
        q.setParameter("expDate",_minExpDate ); // constant value :3
    if (_expired)
        q.setParameter("today", new Date());
    if (genName != null){
        q.setParameter("genName", genName);
    }
    if (_issue)
        q.setParameter("deptIssue", true);

    try{
        tempList = (List <DEPTStoreBalance>) q.getResultList();
    }
    catch (NoResultException nre){
        //do something
    }
    finally {
        em.close();
    }       
    return tempList;        
}

4 个答案:

答案 0 :(得分:1)

Arraylist是一个动态数组。数组具有固定大小,而不是Arraylist。

实施例:    如果您将数组声明为

int[] arr = new int[5];

你必须提供一个数组的大小。

  ArrayList al = new ArrayList();
  if(al.size()>0) { 
   // do your things 

}

答案 1 :(得分:1)

ArrayList只是一个动态增长的数组。

您遇到了一个ArrayIndexOutofBoundException,因为当您收到一个空列表并且您正在尝试访问一个不存在的特定位置 时,JVM会抛出一个例外,抱怨您已经越过内部数组 aka ArrayIndexOutOfBoundException的边界。

例如,在您的情况下,当您有一个空的ArrayList时,列表的大小将为0.但是,如果您尝试访问的索引是&gt; = 0,JVM将抛出一个ArrayIndexOutofBoundException

答案 2 :(得分:0)

ArrayList在内部使用array来存储其内容。 ArrayList基本上是动态的array

如果您尝试访问index中无法使用的元素,您将获得ArrayIndexOutofBoundsException

答案 3 :(得分:0)

如果您的List有3个成员而且您是calling list.get(5),则会抛出异常。这是因为ArrayList是由Java数组实现的。

相关问题