Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java HashMap包含ArrayList的函数_Java_Hashmap - Fatal编程技术网

Java HashMap包含ArrayList的函数

Java HashMap包含ArrayList的函数,java,hashmap,Java,Hashmap,如何将下面函数中的sheetData放入Hashmap 专用静态void showExcelData(列表数据){ for(int i=0;i

如何将下面函数中的
sheetData
放入Hashmap

专用静态void showExcelData(列表数据){

for(int i=0;i
}

程序正在从excel文件读取数据

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;

public class readexcel{

@SuppressWarnings({ "unchecked", "unchecked" })
public static void main(String[] args) throws Exception {

String filename = "C:\\Users\\xxxx\\Documents\\test5.xls";


List sheetData = new ArrayList();
FileInputStream fis = null;
try {

fis = new FileInputStream(filename);


HSSFWorkbook workbook = new HSSFWorkbook(fis);

HSSFSheet sheet = workbook.getSheetAt(0);

Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();

List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}

sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}

showExcelData(sheetData);
}

private static void showExcelData(List sheetData) {

for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
for (int j = 0; j < list.size(); j++) {
Cell cell = (Cell) list.get(j);
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
System.out.print(cell.getNumericCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.print(cell.getRichStringCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
System.out.print(cell.getBooleanCellValue());
 }
if (j < list.size() - 1) {
System.out.print(", ");
}
}
System.out.println("");
 }
 }
 }
import java.io.FileInputStream;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Cell;
公共类readexcel{
@SuppressWarnings({“未选中”、“未选中”})
公共静态void main(字符串[]args)引发异常{
String filename=“C:\\Users\\xxxx\\Documents\\test5.xls”;
List sheetData=new ArrayList();
FileInputStream fis=null;
试一试{
fis=新文件输入流(文件名);
HSSF工作手册=新的HSSF工作手册(fis);
HSSFSheet sheet=工作簿。getSheetAt(0);
迭代器行=sheet.rowIterator();
while(rows.hasNext()){
HSSFRow行=(HSSFRow)行。下一步();
迭代器单元格=行。单元格迭代器();
列表数据=新的ArrayList();
while(cells.hasNext()){
HSSFCell单元格=(HSSFCell)单元格。下一步();
添加数据(单元格);
}
sheetData.add(数据);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
如果(fis!=null){
fis.close();
}
}
showceldata(sheetData);
}
专用静态void showExcelData(列表数据){
对于(int i=0;i
不清楚,但我想你是在问如何使用策略设计模式,所以你使用了
地图
,而不是
如果
如果
,则使用

所以你需要

  • 编写表示抽象操作的接口或抽象基类
  • 编写类型到操作的映射(将单元格类型映射到操作)
  • 更改
    showExcelData
    方法以使用该映射查找要使用的操作,然后委托给该操作

  • 但我不会为您编写代码,因为这不是一项代码编写服务。

    请正确缩进代码,否则很难阅读。您是否尝试过使用单元格索引并将单元格映射到该索引的方法?单元格对象中包含什么,您迄今为止尝试过什么?即使使用更新后,我仍然不知道单元格对象中包含了什么。如果是自定义对象,您不能创建一个要映射到的索引吗?
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Cell;
    
    public class readexcel{
    
    @SuppressWarnings({ "unchecked", "unchecked" })
    public static void main(String[] args) throws Exception {
    
    String filename = "C:\\Users\\xxxx\\Documents\\test5.xls";
    
    
    List sheetData = new ArrayList();
    FileInputStream fis = null;
    try {
    
    fis = new FileInputStream(filename);
    
    
    HSSFWorkbook workbook = new HSSFWorkbook(fis);
    
    HSSFSheet sheet = workbook.getSheetAt(0);
    
    Iterator rows = sheet.rowIterator();
    while (rows.hasNext()) {
    HSSFRow row = (HSSFRow) rows.next();
    Iterator cells = row.cellIterator();
    
    List data = new ArrayList();
    while (cells.hasNext()) {
    HSSFCell cell = (HSSFCell) cells.next();
    data.add(cell);
    }
    
    sheetData.add(data);
    }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (fis != null) {
    fis.close();
    }
    }
    
    showExcelData(sheetData);
    }
    
    private static void showExcelData(List sheetData) {
    
    for (int i = 0; i < sheetData.size(); i++) {
    List list = (List) sheetData.get(i);
    for (int j = 0; j < list.size(); j++) {
    Cell cell = (Cell) list.get(j);
    if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
    System.out.print(cell.getNumericCellValue());
    } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
    System.out.print(cell.getRichStringCellValue());
    } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
    System.out.print(cell.getBooleanCellValue());
     }
    if (j < list.size() - 1) {
    System.out.print(", ");
    }
    }
    System.out.println("");
     }
     }
     }