解决遍历List时都只能取到list的最后一条数据

后台controller中的生成List的部分

List<String> majorsName = classService.findAllMajorsByYear(clGrade);
		MajorAnalysisData majorAnalysisData = new MajorAnalysisData();    //注意这个地方就是错误的原因,把对象创建放在了循环的外面,这样在循环里面给对象赋值时,每循环一次都会覆盖前一次的值
		for (int i =0;i<majorsName.size();i++) {
			
			//设置专业名称
			majorAnalysisData.setClMajor(majorsName.get(i));
			//设置专业人数
			List<Student> studentlist = studentService.findAllStudentsByClMajorAndStGrade(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMaStudentNum(studentlist.size());
			//设置专业挂科人数
			List<Student> failStudentlist = studentService.findFailStudent(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMaFailStudentNum(failStudentlist.size());
			//设置补考未通过人数
			List<Student> makeupFailStudentlist = studentService.makeupFailStudent(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMakeupFailNum(makeupFailStudentlist.size());
			//设置补考通过人数
			majorAnalysisData.setMakeupSuccessNum(failStudentlist.size()-makeupFailStudentlist.size());
			//设置重修人数
			majorAnalysisData.setRebuildNum(failStudentlist.size()-makeupFailStudentlist.size());
			majorAnalysisDatalist.add(majorAnalysisData);
			System.out.println(majorAnalysisData.toString());
		}

前端通过get发送请求,取list里面的值,并赋值给表格

$.get(url,grade,function(data){
	 			var table_makeup = document.getElementById("table_makeup");
				var body_makeup = document.getElementById("body_makeup");
				
				var table_rebuild = document.getElementById("table_rebuild");
				var body_rebuild = document.getElementById("body_rebuild");
				for(var i = 0; i < data.length; i++){
					var a = data[i];
					var tr_makeup = document.createElement("tr");

					var td_clMajor = document.createElement("td");
					var td_maStudentNum = document.createElement("td");
					var td_maFailStudentNum = document.createElement("td");
					var td_makeupSuccessNum = document.createElement("td");
					
					td_clMajor.innerHTML =a.clMajor;
					td_maStudentNum.innerHTML = a.maStudentNum;
					td_maFailStudentNum.innerHTML = a.maFailStudentNum;
					td_makeupSuccessNum.innerHTML = a.makeupSuccessNum;
					tr_makeup.appendChild(td_clMajor);
					tr_makeup.appendChild(td_maStudentNum);
					tr_makeup.appendChild(td_maFailStudentNum);
					tr_makeup.appendChild(td_makeupSuccessNum);
					
					body_makeup.appendChild(tr_makeup);
					
				var tr_rebuild= document.createElement("tr");
					
					var td_clMajor = document.createElement("td");
					var td_maStudentNum = document.createElement("td");
					var td_rebuildNum=document.createElement("td");
					td_clMajor.innerHTML =a.clMajor;
					td_maStudentNum.innerHTML = a.maStudentNum;
					td_rebuildNum.innerHTML = a.rebuildNum;
					
					tr_rebuild.appendChild(td_clMajor);
					tr_rebuild.appendChild(td_maStudentNum);
					tr_rebuild.appendChild(td_rebuildNum);
					
					body_rebuild.appendChild(tr_rebuild);
					
				}
				 table_makeup.appendChild(body_makeup);
				 table_rebuild.appendChild(body_rebuild);
				 
	 		});

前端页面显示:

发现都只取到了list中的最后一个值

但是后台打印出来的结果是正确的(这里我还不太清楚为什么后台的数据是正确的,没有覆盖)

 

 

错误是出现在controller中,在向List中存对象时,对象的创建应该放在for循环里面

for (int i =0;i<majorsName.size();i++) {
			MajorAnalysisData majorAnalysisData = new MajorAnalysisData();    #这一行就是解决方法
			//设置专业名称
			majorAnalysisData.setClMajor(majorsName.get(i));
			//设置专业人数
			List<Student> studentlist = studentService.findAllStudentsByClMajorAndStGrade(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMaStudentNum(studentlist.size());
			//设置专业挂科人数
			List<Student> failStudentlist = studentService.findFailStudent(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMaFailStudentNum(failStudentlist.size());
			//设置补考未通过人数
			List<Student> makeupFailStudentlist = studentService.makeupFailStudent(majorsName.get(i), String.valueOf(clGrade));
			majorAnalysisData.setMakeupFailNum(makeupFailStudentlist.size());
			//设置补考通过人数
			majorAnalysisData.setMakeupSuccessNum(failStudentlist.size()-makeupFailStudentlist.size());
			//设置重修人数
			majorAnalysisData.setRebuildNum(failStudentlist.size()-makeupFailStudentlist.size());
			majorAnalysisDatalist.add(majorAnalysisData);
			System.out.println(majorAnalysisData.toString());
		}

 

 

参与评论 您还未登录,请先 登录 后发表或查看评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

王木木同学

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值