代码如下
List<ExpInfo> expInfos = expInfoService
.getRandom(request.getParameter("all") == null ? "" : request
.getParameter("all"));
List<ExpInfos> expinfoList = new ArrayList<ExpInfos>();
for(int i = 0 ; i < expInfos.size() ; i ++){
expinfoList.get(i).setExpId(expInfos.get(i).getExpId());
expinfoList.get(i).setExpName(expInfos.get(i).getExpName());
expinfoList.get(i).setExpSex(expInfos.get(i).getExpSex());
expinfoList.get(i).setExpAge(expInfos.get(i).getExpAge());
expinfoList.get(i).setCategoryId(expInfos.get(i).getCategoryId());
expinfoList.get(i).setCategoryName(expInfos.get(i).getCategoryName());
expinfoList.get(i).setExpRemark(expInfos.get(i).getExpRemark());
expinfoList.get(i).setCheck(expInfos.get(i).getCheck());
}
可是为什么提示超出索引了啊?
是expinfoList超出索引了 你新new的一个list ,size=0 ; expinfoList.get(i).setExpId(expInfos.get(i).getExpId()); 这里超出了索引!
我知道问题出在这,但是我不知道应该怎么解决
应该改为 expinfoList.set(i,expInfos.setExpId(expInfos.get(i).getExpId())); 就没有问题了! 记得采纳
List expInfos = expInfoService
.getRandom(request.getParameter("all") == null ? "" : request
.getParameter("all"));
List expinfoList = new ArrayList();
for(int i = 0 ; i < expInfos.size() ; i ++){
expinfoList.add(expInfos.get(i));
}
List<ExpInfos> expinfoList = new ArrayList<ExpInfos>();
这个新建的list size是0,是个空,在for循环中,通过索引获取元素就会越界,可以修改为如下:
List<ExpInfo> expInfos = expInfoService
.getRandom(request.getParameter("all") == null ? "" : request
.getParameter("all"));
ExpInfos[] expinfoArray = new ExpInfos[expInfos.size()];
Arrays.fill(expinfoArray, new ExpInfos());
List<ExpInfos> expinfoList = Arrays.asList(expinfoArray);
for(int i = 0 ; i < expInfos.size() ; i ++){
expinfoList.get(i).setExpId(expInfos.get(i).getExpId());
expinfoList.get(i).setExpName(expInfos.get(i).getExpName());
expinfoList.get(i).setExpSex(expInfos.get(i).getExpSex());
expinfoList.get(i).setExpAge(expInfos.get(i).getExpAge());
expinfoList.get(i).setCategoryId(expInfos.get(i).getCategoryId());
expinfoList.get(i).setCategoryName(expInfos.get(i).getCategoryName());
expinfoList.get(i).setExpRemark(expInfos.get(i).getExpRemark());
expinfoList.get(i).setCheck(expInfos.get(i).getCheck());
}
或者修改为如下的方式:
List<ExpInfo> expInfos = expInfoService
.getRandom(request.getParameter("all") == null ? "" : request
.getParameter("all"));
List<ExpInfos> expinfoList = new ArrayList<ExpInfos>();
for (ExpInfo expInfo : expInfos) {
ExpInfos expInfosModel = new ExpInfos();
expInfosModel.setExpId(expInfo.getExpId());
expInfosModel.setExpId(expInfo.getExpId());
expInfosModel.setExpName(expInfo.getExpName());
expInfosModel.setExpSex(expInfo.getExpSex());
expInfosModel.setExpAge(expInfo.getExpAge());
expInfosModel.setCategoryId(expInfo.getCategoryId());
expInfosModel.setCategoryName(expInfo.getCategoryName());
expInfosModel.setExpRemark(expInfo.getExpRemark());
expInfosModel.setCheck(expInfo.getCheck());
expinfoList.add(expInfosModel);
}
expinfoList超出索引导致的这个问题
List expinfoList = new ArrayList();
只是新建了一个List,你可以这样试试
List expinfoList = new ArrayList();
for(int i = 0 ; i < expinfoList.size() ; i ++){
你可以对expInfos.size()的进行判断,如果等于0,则不进行操作,大于0时才进行以下操作:
List expinfoList = new ArrayList();
for(int i = 0 ; i < expInfos.size() ; i ++){
expinfoList.get(i).setExpId(expInfos.get(i).getExpId());
expinfoList.get(i).setExpName(expInfos.get(i).getExpName());
expinfoList.get(i).setExpSex(expInfos.get(i).getExpSex());
expinfoList.get(i).setExpAge(expInfos.get(i).getExpAge());
expinfoList.get(i).setCategoryId(expInfos.get(i).getCategoryId());
expinfoList.get(i).setCategoryName(expInfos.get(i).getCategoryName());
expinfoList.get(i).setExpRemark(expInfos.get(i).getExpRemark());
expinfoList.get(i).setCheck(expInfos.get(i).getCheck());
}
List<ExpInfos> expinfoList = new ArrayList<ExpInfos>();
for(int i = 0 ; i < expInfos.size() ; i ++)
这时expinfoList的大小就是0
索引超出下线,你的list大小为0