for循环中ArrayLis嵌套用ArrayList的一些坑

在写项目时遇到从数据库取出一堆关于评论的信息,在Service层中进行数据处理,最终想以List<List<Map<>>>>形式进行解析交互。

先看我正确的代码,之后在放出遇到的坑,


package com.platebbg.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platebbg.product.dao.CommentProductDao;
import com.platebbg.product.dao.dto.ProductComment;
import com.platebbg.product.service.CommentProductService;
@Service
public class CommentProductServiceImpl implements CommentProductService{
	@Autowired
	private CommentProductDao commentProductDao;
	@Override
	public List< List<Map<String, Object>>> commentproductInfo(int plateId) {
		List< List<Map<String, Object>>> result=new ArrayList<>();
		List<Map<String, Object>> resultlist=new ArrayList<>();
		List<ProductComment> commentlist=commentProductDao.getCommentByplateId(plateId);
		int temp=commentlist.get(0).getCommentid();
		for(int i=0;i<commentlist.size();i++){
			ProductComment productcomment =commentlist.get(i);
			Map<String,Object> productresult=new HashMap<>();
			Integer commentId=productcomment.getCommentid();
			System.out.println(commentId);
			System.out.println(temp);
			if(commentId==temp){
				System.out.println("11111111111");
				productresult.put("commentId", commentId);
				productresult.put("commentlevel", productcomment.getCommentlevel());
				productresult.put("commenttitle", productcomment.getCommenttitle());
				productresult.put("commentuser", productcomment.getCommentuser());
				productresult.put("commentphone", productcomment.getCommentphone());
				productresult.put("commenttime", productcomment.getCommenttime());
				productresult.put("comsubId",productcomment.getComsubid());
				productresult.put("comsubcontent", productcomment.getComsubcontent());
				productresult.put("comsubimg", productcomment.getComsubimg());
				productresult.put("comsubuser",productcomment.getComsubuser() );
				productresult.put("comsubphone", productcomment.getComsubphone());
				productresult.put("comsubtime", productcomment.getComsubtime());
				resultlist.add(productresult);	
							
			}
			else{
				System.out.println("222222222222");
				result.add(resultlist);
				resultlist=new ArrayList<>();
				productresult.put("commentId", commentId);
				productresult.put("commentlevel", productcomment.getCommentlevel());
				productresult.put("commenttitle", productcomment.getCommenttitle());
				productresult.put("commentuser", productcomment.getCommentuser());
				productresult.put("commentphone", productcomment.getCommentphone());
				productresult.put("commenttime", productcomment.getCommenttime());
				productresult.put("comsubId",productcomment.getComsubid());
				productresult.put("comsubcontent", productcomment.getComsubcontent());
				productresult.put("comsubimg", productcomment.getComsubimg());
				productresult.put("comsubuser",productcomment.getComsubuser() );
				productresult.put("comsubphone", productcomment.getComsubphone());
				productresult.put("comsubtime", productcomment.getComsubtime());
				resultlist.add(productresult);
				temp=commentId;
			}
			
		}
		result.add(resultlist);	
		return result;
	}

}



不知大家主要到没有,我这使用的

resultlist=new ArrayList<>();
是重新new一个对象,在之前我想着不要重复创建新对象,便使用了

resultlist.clear();

来进行清除处理,但是传到页面上的数据会都是一样的数据,我反复测验,表示在传到Service层的数据没有错误,

之后终于发现list里面放的是同一个对象map的堆地址,指向的都是同一个对象

所以就算你clear之后,你之前的result.add中存放进去的ArrayList的对象地址并没有改变,就是说你clear之后在放进去值,之前已经放入result中的ArrayList也会跟着改变,
最后之后变成都是最后改变的resultlist的值。


这是我在开发评论模块中遇到的问题,不喜勿喷。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值