List 和 Array之间有什么关系。为什么我得到 ArrayIndexOutOfBoundsException.?
基本上,我将一个参数传递给一个方法,基于使用 HQL 的方法,我返回一组ArrayList。现在我的问题是。当这个 ArrayList 返回大于 0(大小)时,这正是我想要的方式。
但是当它返回 0(大小)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;
}