[英]How to store into an array, in Java, the rows of another array that contain a word
我有一个数组,它以相同的顺序包含 Excel 表中的所有数据。 我想将数组第 4 列中包含“执行”一词的行保存在一个数组中。 你认为你能指导我吗? 在这里,我给你留下一个参考和迄今为止构建的方法。
我的数组看起来像这样,它的名字是'dataTable':
月 | 概括 | 卡片 | 短跑 | 行动 | 关 |
---|---|---|---|---|---|
四月 | 一 | 使固定 | 1 | 执行 | 是的 |
可能 | 二 | 脚本 | 2 | 不适用 | 不 |
六月 | 三 | 使固定 | 3 | 执行 | 是的 |
另一个数组应如下所示,其名称为“执行”:
月 | 概括 | 卡片 | 短跑 | 行动 | 关 |
---|---|---|---|---|---|
四月 | 一 | 使固定 | 1 | 执行 | 是的 |
六月 | 三 | 使固定 | 3 | 执行 | 是的 |
我的代码是:
public static int numToExecute() {
executions = 0;
for (String[] row : dataTable) {
if (header == false) {
for (String field : row) {
if (field.equals("execute")) {
executions++;
}
}
}
header = false;
}
execute = new String[executions][dataTable[0].length];
//System.out.println(executions);
return executions;
}
//Method to transfer rows from array 'dataTable' to array 'execute'
public static String listToExecute() {
//The dataTable array already contains all the data of the excel table, including the header
execute = new String[numToExecute()][dataTable[0].length];
for (int i = 1; i < dataTable.length; i++) {//I initialize i to 1 because I don't want to extract the header from the table
System.out.println("***************EXECUTION*******************" + dataTable.length);
for (int j = 0; j < dataTable[0].length; j++) {
if (dataTable[i][4].contains("execute")) {//Here I filter the rows that contain "execute" in column 4
execute[i - 1][j] = dataTable[i][j];//<---This is where it fails: I put 'i-1' because I want the first row to be filtered
//be stored at execute[0][0]
System.out.println(execute[i - 1][j]);//It should print every value stored at the 'execute' array
}
continue;
}
}
return "";
}
我用来读取 Excel 表并将其保存到 dataTable 数组的代码是:
public static String readExcel(String fileName, String sheetName) {
// Create a file input stream to read Excel workbook and worksheet
xlFile = new File(".\\" + fileName);
fis = new FileInputStream(xlFile);
xlWB = new XSSFWorkbook(fis);
xlSheet = xlWB.getSheet(sheetName);
// Get the number of rows and columns
int numRows = xlSheet.getLastRowNum() + 1;
int numCols = xlSheet.getRow(0).getLastCellNum();
// Create double array data table - rows x cols
// We will return this data table
dataTable = new String[numRows][numCols];
// For each row, create a HSSFRow, then iterate through the "columns"
// For each "column" create an HSSFCell to grab the value at the specified cell
// (i,j)
for (int i = 1; i < numRows; i++) {
XSSFRow xlRow = xlSheet.getRow(i);
//System.out.println("**********************************************");
for (int j = 0; j < numCols; j++) {
XSSFCell cell = xlRow.getCell(j);
dataTable[i][j] = cell.toString();
//System.out.println(dataTable[i][j]);
}
}
}
我得到的结果是:
你可以使用这个 -
public static void main(String[] args) {
String[][] dataTable = {{"MONTH", "SUMMARY", "CARD", "SPRINT", "ACTION", "CLOSE"},
{"april", "one", "fix", "1", "execute", "yes"},
{"may", "two", "script", "2", null, "no"},
{"june", "three", "fix", "3", "execute", "yes"}};
List<String[]> outList = new ArrayList<>();
int i = 0;
for (String[] row : dataTable) {
if (i == 0 || null != row[4] && row[4].contains("execute")) {
outList.add(row);
}
i++;
}
for (String[] aa : outList) {
System.out.println(Arrays.toString(aa));
}
String[][] arrayFormat = outList.toArray(new String[0][0]);
for (String[] aa : arrayFormat) {
System.out.println(Arrays.toString(aa));
}
}
同样,在填充数据表时,您首先从 1 而不是 0 开始 for 循环,因此,我认为您在数据表中缺少标题,而且您正在使用任何 null 检查进行 cell.toString(),因此您可能会得到 NullPointer,下面是增强的for 循环 -
for (int i = 0; i < numRows; i++) {
XSSFRow xlRow = xlSheet.getRow(i);
for (int j = 0; j < numCols; j++) {
XSSFCell cell = xlRow.getCell(j);
if (null != cell) {
dataTable[i][j] = cell.toString();
} else {
dataTable[i][j] = null;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.