java map<string_ object> 遍历_List<Map<String, Object>> 遍历

要把List>里面

key为"Name", "Phone", "daan"

获取然后放到List list

Question.java

public class Question {

public String question;

public String answerA;

public String answerB;

public String answerC;

public String answerD;

public int answer;

public String explaination;

public int ID;

public int selectedAnswer;

}

我的实现:

private static List> list_data;

public void init(Context context, String eXCEL_PATH) {

list_data = FileUtils.readExcel(context, eXCEL_PATH);

}

public List getQuestions() {

List list = new ArrayList();

for (int i = 0; i < list_data.size(); i++) {

Question question = new Question();

// HashMap map = (HashMap) list_data.get(i);

Map map = list_data.get(i);

Set set = map.keySet();

for (String key:set) {

question.question = (String) map.get("Name");

String name = null;

name = (String) map.get("Phone");

String[] tmp = name.split(";");

question.answerA = tmp[0];

question.answerB = tmp[1];

question.answerC = tmp[2];

question.answerD = tmp[3];

question.explaination = (String) map.get("daan");

}

question.selectedAnswer = -1;

list.add(question);

}

return list;

}

FileUtils.java

public static List> readExcel(Context context, String path) {

List> data = new ArrayList<>();

try {

InputStream is = new FileInputStream(path);

Workbook book = Workbook.getWorkbook(is);

Sheet sheet = book.getSheet(0);

int Rows = sheet.getRows(); // 行

int Cols = sheet.getColumns(); // 列

int colsOfName = 0;

int rolsOfName = 0;

int colsOfNum = 0;

int rolsOfNum = 0;

int colsdaan = 0;

int rolsdaan = 0;

for (int i = 0; i < Cols; ++i) {

for (int j = 0; j < Rows; ++j) {

if (sheet.getCell(i, j).getContents().equals("a")) {

colsOfName = i;

rolsOfName = j;

break;

}

}

}

for (int i = 0; i < Cols; ++i) {

for (int j = 0; j < Rows; ++j) {

if (sheet.getCell(i, j).getContents().equals("b")) {

colsOfNum = i;

rolsOfNum = j;

break;

}

}

}

for (int i = 0; i < Cols; ++i) {

for (int j = 0; j < Rows; ++j) {

if (sheet.getCell(i, j).getContents().equals("c")) {

colsdaan = i;

rolsdaan = j;

break;

}

}

}

Map map = new HashMap<>();

for (int i1 = rolsOfName + 1, i2 = rolsOfNum + 1, i3 = rolsdaan + 1; i1 < (Rows - rolsOfName); i1++, i2++, i3++) {

if (!sheet.getCell(colsOfName, i1).getContents().equals("") && !sheet.getCell(colsOfNum, i2).getContents().equals("") && !sheet.getCell(colsdaan, i3).getContents().equals("")) {

map.put("Name", sheet.getCell(colsOfName, i1).getContents());

map.put("Phone", sheet.getCell(colsOfNum, i2).getContents());

map.put("daan", sheet.getCell(colsdaan, i3).getContents());

/*

Question question = new Question();

question.question = sheet.getCell(colsOfName, i1).getContents();

String name = null;

name = sheet.getCell(colsOfNum, i2).getContents();

String[] tmp = name.split(";");

question.answerA = tmp[0];

question.answerB = tmp[1];

question.answerC = tmp[2];

question.answerD = tmp[3];

question.explaination = sheet.getCell(colsdaan, i3).getContents();

question.selectedAnswer = -1;

list.add(question);

*/

data.add(map);

}

}

book.close();

} catch (Exception e) {

Log.i("log", e + "");

}

return data;

}

问题:

开始的时候报arrayindexoutofboundsexception

然后修改代码如上

的到的是却是遍历的最后一个值

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值