2012-09-21 4 views
-2

리스트와 배열 사이의 관계는 무엇입니까? 왜 ArrayIndexOutOfBoundsException가 발생합니까? Arraylist와 ArrayIndexOutOfBoundsException 사이의 관계

기본적 나 그 사용 HQL에 기초하는 방법에 하나 개의 인자를 통과하고 난 ArrayList를 한 세트를 반환하고있다. 이제 제 문제가 있습니다. 이 ArrayList 0 (size) 이상을 반환 할 때이 정확히 원하는 방식으로 작동합니다.

하지만 0 (크기) ArrayList를 반환하면 예외가 발생합니다. 왜 그런가? 어떤 몸이 나에게 설명 할 수


지금 내가 ArrayList에 5가 (처음) & 0 내가 어떤 예외를 받고 있지 않다 (제 2의 사이즈로 난까지 테스트)를 돌려주는 경우처럼 한 번 더 의심을 얻었다. 하지만 그것은 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;   
} 
+2

오류를 생성하는 코드를 게시 할 수 있습니까? – Keppil

+0

아무도 나에게이 질문을 설명 할 수 있습니까? –

+0

내가 언급 한대로 하나의 배열 목록을 반환합니다. 배열 목록에서 배열을 지우지 않고 처음으로 5 개의 요소를 반환하고 다음에 0 개의 요소를 반환하면 예외가 발생하지 않습니다.
그러나 배열 목록에서 다음 번에 200 번째 요소와 첫 번째 요소와 같은 요소가 더 많이 반환되면 예외가 발생합니다.
처음 두 번째로 의미하는 것은 검색 구성 요소에 배열을 사용하고 있음을 의미합니다. 배열 목록을 지우지 않고 항목을 검색하면 예외가 발생합니다.
왜 그런가요? 그게 내 질문이야. – Pesula

답변

0

ArrayList 내부적으로 컨텐츠를 저장하기 위해 array를 사용합니다. ArrayList은 기본적으로 일종의 동적 인 array입니다.

index에서 사용할 수없는 요소에 액세스하려고 시도하면 ArrayIndexOutofBoundsException이 표시됩니다.

1

Arraylist는 동적 배열입니다. 배열은 Arraylist가 아닌 고정 크기입니다.

예 : 당신이

int[] arr = new int[5]; 

으로 배열을 선언 할 경우 배열의 크기를 제공해야합니다.

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

} 
1

ArrayList는 동적으로 늘어나는 배열입니다.

당신은 빈 목록을 받아 당신이 존재하지 않는입니다 특정 위치 에 액세스하려고 할 때, JVM은 당신이 (가)의 경계 교차 한 불평하면 예외가 발생하기 때문에이 ArrayIndexOutofBoundException가 발생하는

내부 배열 ArrayIndexOutOfBoundException. 당신은 빈의 ArrayList를했습니다 때 JVM이 ArrayIndexOutofBoundException가 발생합니다> = 0 인 인덱스에 액세스하려고하면

예를 들어, 귀하의 경우, 목록의 크기는 그러나 0이 될 것이다

+0

빈 배열에서 인덱스 0에 액세스하면 ArrayIndexOutOfBoundsException이 발생합니다. –

+0

@ SimonAndréForsberg : 정확하고 직설적 인 생각이 아니 었습니다. 지적 해 주셔서 감사합니다! – Sujay

+0

안녕하세요 @Sujay & @ Simon André Forsberg, ur 답변에 감사드립니다. arraylist가 5를 반환하면 (처음으로) & 0 (테스트 한 크기로 두 번째) 나는 어떤 예외도 얻지 못하고있는 것처럼 이제는 의심이 하나 더 있습니다. 하지만 그것은 200 개 이상의 요소를 반환 할 때 예외가 발생합니다. 그게 왜? '특정 상수 번호가 있습니까? 이처럼 많은 요소 배열이 IndexOutofBounds와 같은 것을 제공해서는 안된다. '나에게 설명 할 수 있을까? – Pesula

0

List에 3 명의 회원이 있고 calling list.get(5) 인 경우 예외가 발생합니다. ArrayList이 Java 배열로 구현 되었기 때문입니다.