ArrayIndexOutofBoundsException处理数组

我想从Excel文件读取数字,并把它们放入数组,但我得到一个越界的例外…在Excel文件有2列,每列32行…在第一列(这是5位长)将被插入到名为allInputs的数组中,而第二列(长度为3位)的数据将被插入名为allTargets的数组中…以下是我的代码;

String[] allInputs = new String[32]; String[] allTargets= new String[32]; //Read the values from csv file //Input file which needs to be parsed String fileToParse = "C:\\Users\\ROBMARYAN\\Documents\\UOM\\Bsc IT Comp & Business\\3rd Yr\\Business Intelligence\\NeuralNetAssignment\\src\\inputs.csv"; BufferedReader fileReader = null; //Delimiter used in CSV file final String DELIMITER = ","; try { String line = ""; //Create the file reader fileReader = new BufferedReader(new FileReader(fileToParse)); //Read the file line by line int icount=0; //determine where to store the input int tcount=0; //determine where to store the target while ((line = fileReader.readLine()) != null) { //Get all tokens available in line String[] tokens = line.split(DELIMITER); for(String token : tokens) { //Print all tokens if (token.length() == 5) { ***allInputs[icount]=token***; System.out.println("Stored in all inputs:"+allInputs[icount]); icount++; } else { allTargets[tcount]=token; System.out.println("Stored in all targets:"+allTargets[tcount]); tcount++; } System.out.println("iCOUNT:"+icount); System.out.println("tCOUNT:"+icount); } } } catch (Exception e) { e.printStackTrace(); } finally { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } 

allInputs[icount]=token ,我得到以下错误;

java.lang.ArrayIndexOutOfBoundsException: 32

提前致谢!

如果单元格的内容长度是5,那么这行代码将在每个单元格中运行一次。由于您的电子表格是32 x 2,因此可能会有多达64个这样的string。但是您试图将它们放入一个长度为32的数组中当然会超越其界限。