2016-06-15 4 views
-3

이 예외에 대한 모든 질문을 보았습니다. 배열 인덱스가 0부터 시작하고 크기가 1 인 경우 인덱스 0에만 액세스 할 수 있다는 것을 알고 있습니다.이 논리를 알고 있지만 다음 코드를 수정할 수 없습니다.인덱스 1, 크기 1 예외

private static Map<String, Map<String, List<String>>> executions = new HashMap<String, Map<String, List<String>>>(); 

public static String readExecution(String jobName, String deviceName, int executionNumber) { 
    String result = null; 
    Map<String, List<String>> jobExecutions = executions.get(jobName); 
    if (jobExecutions != null) { 
     List<String> executionPerDevice = jobExecutions.get(deviceName); 
     result = executionPerDevice.get(executionNumber); // ERROR 
    } 

    return result; 
    } 

편집 : Fildor의 comment.execution 번호의 대답은 1

enter image description here 어떤 도움이다 ? 감사합니다.

+4

디버그를 :이 숫자가 어쨌든 executionPerDevice 크기에 맞는 경우 확인해야하므로 실행 번호는 인수로 외부에서 전달됩니다. 'executionNumber'가 "1"이라고 말하고 싶습니다. 만약 당신이 1로 번호 매기기를 시작한다면,이'result = executionPerDevice.get (executionNumber-1);을 만들고, 또한 몇몇 nullchecks를 할 것입니다. – Fildor

+0

executionPerDevice의 크기를 확인하고 executionNumber와 비교할 수 있습니다. –

+0

IndexOutOfBound 예외는 매우 쉬운 예외입니다. 존재하지 않는 것을 얻기 위해 노력하고 있습니다. 코드를 가져 와서 왜 값이 없는지 알아 내려면 코드를 디버깅하십시오. – john

답변

1

빠른 솔루션 :

 if (executionPerDevice.size() >= executionNumber){ 
      result = executionPerDevice.get(executionNumber); 
     } 
+0

두 경계를 모두 확인하는 것을 잊지 마십시오 ... – Fildor

+0

@이 답변은 내 문제를 해결했습니다. – limonik

+0

테스트가 잘못되었습니다. 엄격해야합니다. –

0

이러한 런타임 예외 메시지는 을 이해하는 경향이 있습니다.이 문제를 이해하려면을 알아야합니다.

크기는 1입니다.

INDEX가 하나입니다.

크기가 1 인 목록에 대해 유효한 인덱스 집합을 고려하십시오.

관련 문제