2014-10-01 14 views
3

나는 택시 물건을 잡을 arraylist를 만드는 프로그램을 가지고있다. 나는 메시지에서 얻은 것이 자바가 arraylist에 객체를 가지고 있다는 것을 인식하지 못한다는 오류를 계속 발생시킨다. 이것은 내가 얻고있는 오류입니다. 스레드에서Java에서 ArrayList의 요소를 인식하지 못합니까?

예외 "주요"java.lang.IndexOutOfBoundsException : 인덱스 : 20, 크기 : java.util.ArrayList.rangeCheck 20
(알 수없는 소스) java.util.ArrayList.get에서
(이것은 내가 일을하려고 노력하고있는 코드가

알 수없는 소스) edu.Tridenttech.MartiC.app.CabOrginazer.main (CabOrginazer.java:48에서
)

public class CabOrginazer { 

private static List<CabProperties> cabs = new ArrayList<CabProperties>(); 
private static int count = 0; 
private static boolean found = false; 


public void cabOrginazer() 
{ 

} 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    CabRecordReaper reaper = new CabRecordReaper("C:/CabRecords/September.txt"); 
    CabProperties cabNum; 

    for(int i = 0; i < 20; i++) 
    { 
     cabNum = new CabProperties(); 
     cabs.add(cabNum); 
    } 
    while(reaper.hasMoreRecords()) 
    { 
      CabRecord file = reaper.getNextRecord(); 
      for(int j = 0; j < cabs.size(); j++) 
      { 
       if(cabs.get(j).getCabID() == file.getCabId()) 
       { 
        found = true; 
        cabs.get(j).setTypeAndValue(file.getType(), file.getValue(), file.getPerGallonCost()); 
        cabs.get(j).setDate(file.getDateString()); 
        break; 
       } 

      } 

      if(found == false) 
      { 
       cabs.get(count).setCabId(file.getCabId()); 
       count++; 
      } 
      /*for(CabProperties taxi : cabs) 
      { 
       if(taxi.getCabID() == file.getCabId()) 
       { 
        found = true; 
        taxi.setTypeAndValue(file.getType(), file.getValue(), file.getPerGallonCost()); 
        taxi.setDate(file.getDateString()); 
        break; 
       } 


      }*/ 

    } 


    for(CabProperties taxi : cabs) 
    { 
     System.out.print("cab ID: " + taxi.getCabID()); 
     System.out.print("\tGross earning: " + taxi.getGrossEarn()); 
     System.out.print("\tTotal Gas Cost: " + taxi.getGasCost()); 
     System.out.print("\tTotal Service Cost: " + taxi.getServiceCost()); 
     System.out.println(); 

    } 


} 

} 

라인 48는 그 경우의 진술입니다. cabs.get(count).setCabId(file.getCabId()); 나는 자바에 대해 조금 알고 있습니다. Java는 cabs 안에 요소가 있다는 것을 알아야하며 그 중 id을 택할 수 있어야합니다. Java로 인해 arraylist가 채워지는 것을 인식하지 못하게 할 수 있습니까?

답변

7

목록 count의 요소로 채워지지 않은이 채워지지 않습니다. 예외를 살펴보십시오. 목록에 20 개의 요소가 있으므로 유효한 인덱스는 0에서 19까지입니다. 20 번째 레코드 (즉, 21 번째 레코드)를 요청하고 있습니다. 존재하지 않습니다. 더미 속성리스트의 초기 인구 - 당신은 잘 전적으로 count 변수를 제거 할 수 있습니다

if (!found) 
{ 
    CabProperties properties = new CabProperties(); 
    properties.setCabId(file.getCabId()); 
    // Probably set more stuff 
    cabs.add(properties); 
} 

: 당신의 블록이 뭔가를해야처럼

는 소리가 난다. 처음에는 그런 목록을 채우는 것이 이상합니다. 보통 배열으로 고정 크기를 사용하는 것이 좋습니다. ArrayList과 같은 List을 사용하면 얻을 수있는 주요 이점 중 하나는 동적 크기를가집니다.

4

Java는 구성원을 올바르게 인식하고 있습니다. 배열에 인덱스 0에서 인덱스 19까지 색인 된 20 개의 구성원이 있습니다.

존재하지 않는 색인 20을 요청하고 있습니다.

에 대한 루프 : 예상보다

while(reaper.hasMoreRecords()) 

는 더 많은 시간을 실행해야하며 데이터가 타격하는 found == false 경우 여러 번 (그냥 if (!found) { ...을 말할 수있다), 그리고 21 시간의 조건 그것은 인덱스 아웃 오브 바운드 예외로 실패합니다.

디버거 사용법도 알아야합니다.

관련 문제