尝试访问ArrayList的ArrayList元素时的IndexOutOfBoundsException

时间:2017-08-27 18:04:18

标签: java

我正在尝试编写Hackerrank问题https://www.hackerrank.com/challenges/dynamic-array/problem每当我尝试实现此代码时,我得到此java.lang.IndexOutOfBoundsException,但结果(x ^ lastAswer)%N小于N且完全在范围:

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int N = sc. nextInt(); //no. of sequences
    int Q = sc.nextInt(); //no. of queries

    int lastAnswer = 0;

    List<List<Integer>> seq = new ArrayList<List<Integer>>(N);

    while(Q>0){
        int Qtype = sc.nextInt();
        int x = sc.nextInt();
        int y = sc.nextInt();
        if(Qtype == 1){
            seq.get((x^lastAnswer)%N).add(y);
        }
        else{
            seq.get((x^lastAnswer)%N).add(y);
            lastAnswer=y;
            System.out.println(lastAnswer);
        }
        Q--;
    }
    sc.close();   
}

0 个答案:

没有答案