存储到2D数组时发生Java数组索引越界异常
我的Excel包含这样的值存储到2D数组时发生Java数组索引越界异常,java,Java,我的Excel包含这样的值 第1行:测试用例名称 第2行:标题参数值 第3行:IE的参数 第4行:Chrome的参数 第5行:Firefox的参数 我提取每行值并将其存储在LinkedHashMap中。我正在创建一个2D数组,并将此linkedHashmap存储到该2D数组中 它正在返回索引越界异常。任何一个都可以将表值存储到StoreAllArray中吗 package com.xchanging.selenium.utility; import java.io.IOException; i
package com.xchanging.selenium.utility;
import java.io.IOException;
import java.util.LinkedHashMap;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public static LinkedHashMap<String, String> getData(String sheetName,
String testCaseName) throws IOException {
System.out.println("Entry Point");
XSSFWorkbook sourceBook = new XSSFWorkbook("./TestCases.xlsx");
XSSFSheet sourceSheet = sourceBook.getSheet(sheetName);
int testCaseStartRow = 0;
while (!sourceSheet.getRow(testCaseStartRow).getCell(0)
.getRichStringCellValue().toString().equals(testCaseName)) {
testCaseStartRow++;
}
System.out.println("testCaseStartingRow: " + testCaseStartRow);
int testCaseStartColumn = 0;
int headerRow = testCaseStartRow + 1;
int keyRow = headerRow + 1;
while (sourceSheet.getRow(headerRow).getCell(testCaseStartColumn) != null) {
testCaseStartColumn++;
}
int lastColumn = testCaseStartColumn - 1;
// int parameters = lastColumn + 1;
int numberofRows = keyRow;
System.out.println("Keys Start Row" + keyRow);
while (!sourceSheet.getRow(numberofRows).getCell(0).toString()
.equalsIgnoreCase("End")) {
System.out.println(sourceSheet.getRow(numberofRows).getCell(0));
numberofRows++;
}
System.out.println("numberofRows " + numberofRows);
LinkedHashMap<String, String> table = new LinkedHashMap<String, String>();
// Object[][] testData = new Object[0][lastColumn];
Object[][] storeAllArray = new Object[0][3];
int count = 0;
for (int i = keyRow; i < numberofRows; i++) {
for (int j = 0; j <= lastColumn; j++) {
int cellType = sourceSheet.getRow(i).getCell(j).getCellType();
if (cellType == 0) {
String key = sourceSheet.getRow(headerRow).getCell(j)
.toString();
String value = Double.toString(sourceSheet.getRow(i)
.getCell(j).getNumericCellValue());
table.put(key, value);
} else if (cellType == 1) {
String key = sourceSheet.getRow(headerRow).getCell(j)
.toString();
String value = sourceSheet.getRow(i).getCell(j).toString();
table.put(key, value);
} else if (cellType == 2) {
String key = sourceSheet.getRow(headerRow).getCell(j)
.toString();
String value = sourceSheet.getRow(i).getCell(j)
.getRawValue().toString();
table.put(key, value);
} else if (cellType == 4) {
String key = sourceSheet.getRow(headerRow).getCell(j)
.toString();
String value = Boolean.toString(sourceSheet.getRow(i)
.getCell(j).getBooleanCellValue());
table.put(key, value);
} else {
String key = sourceSheet.getRow(headerRow).getCell(j)
.toString();
String value = "";
table.put(key, value);
}
}
storeAllArray[0][count] = table;
count++;
}
return table;
}
}
package com.xchangg.selenium.utility;
导入java.io.IOException;
导入java.util.LinkedHashMap;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类ReadExcel{
公共静态LinkedHashMap getData(字符串名称,
字符串testCaseName)引发IOException{
System.out.println(“入口点”);
XSSFWorkbook sourceBook=newXSSFWorkbook(“./TestCases.xlsx”);
XSSFSheet sourceSheet=sourceBook.getSheet(sheetName);
int testCaseStartRow=0;
而(!sourceSheet.getRow(testCaseStartRow).getCell(0)
.getRichStringCellValue().toString().equals(testCaseName)){
testCaseStartRow++;
}
System.out.println(“testCaseStartingRow:+testCaseStartRow”);
int testCaseStartColumn=0;
int headerRow=testCaseStartRow+1;
int keyRow=headerRow+1;
while(sourceSheet.getRow(headerRow).getCell(testCaseStartColumn)!=null){
testCaseStartColumn++;
}
int lastColumn=testCaseStartColumn-1;
//int参数=最后一列+1;
int numberofRows=keyRow;
System.out.println(“键开始行”+键行);
而(!sourceSheet.getRow(numberofRows).getCell(0).toString()
.equalsIgnoreCase(“结束”){
System.out.println(sourceSheet.getRow(numberofRows.getCell(0));
numberofRows++;
}
System.out.println(“numberofRows”+numberofRows);
LinkedHashMap表=新建LinkedHashMap();
//对象[][]testData=新对象[0][lastColumn];
对象[][]存储阵列=新对象[0][3];
整数计数=0;
for(int i=keyRow;i for(int j=0;jObject[]storeAllArray=new Object[0][3];
是一个包含0
行的数组。您不能在其中放入任何内容,这就是storeAllArray[0][count]
失败的原因
将其更改为:
Object[][] storeAllArray = new Object[1][3];
假设一行就足够了,并且count
永远不会超过2。当然,如果只有一行,则不需要2D数组
import java.util.ArrayIndexOutofBondsException;
import java.util.Scanner;
public class Trycatch2
{
public static void main(String[] args) {
int a[] = {10,20,30};
Scanner sc = new Scanner(System.in);
System.out.println("enter int index[0-2]");
try
{
int index = sc.nextInt();
System.out.println("element="+a[index]);
}
catch(ArrayIndexOutofBondsException e)
{
System.out.println("invalid int index using 0 as default index");
System.out.println("elements="+a[0]);
}
System.out.println("bye");
sc.close();
}
}
enter code here
show error