栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java对象批量赋值时指向同一个地址

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java对象批量赋值时指向同一个地址

问题:
批量赋值时每个List对象都指向同一个地址,修改一个对象的值导致其它下标的内容全修改;这是因为在堆中创建一个对象,其它地方将这个对象赋值给其它对象会传递它的地址而不是值本身。

重写每个层级的clone()方法

@Data
class TestName implements Cloneable {
    public TestName(String nameKey, List dateList) {
        this.nameKey = nameKey;
        this.dateList = dateList;
    }

    String nameKey;
    List dateList;

    protected TestName clone() throws CloneNotSupportedException {
        TestName testName = (TestName) super.clone();
        testName.dateList = new ArrayList<>();
        for (int i = 0; i < dateList.size(); i++) {
            testName.dateList.add(dateList.get(i).clone());
        }
        return testName;
    }

}

@Data
class TestDate implements Cloneable {
    public TestDate(String dateKey, List typeList) {
        this.dateKey = dateKey;
        this.typeList = typeList;
    }

    String dateKey;
    List typeList;

    protected TestDate clone() throws CloneNotSupportedException {
        TestDate testDate = (TestDate) super.clone();
        testDate.typeList = new ArrayList<>();
        for (int i = 0; i < typeList.size(); i++) {
            testDate.typeList.add(typeList.get(i).clone());
        }
        return testDate;
    }

}

@Data
class TestType implements Cloneable {
    public TestType(String typeKey, Integer count) {
        this.typeKey = typeKey;
        this.count = count;
    }

    String typeKey;
    Integer count;

    protected TestType clone() throws CloneNotSupportedException {
        return (TestType) super.clone();
    }

}

赋值代码,add方法时使用clone()方法赋值

public static void main(String[] args) throws Exception {
	List typeList = new ArrayList<>();
	typeList.add(new TestType("imsi", 0));
	typeList.add(new TestType("imei", 0));
	
	List dateList = new ArrayList<>();
	dateList.add(new TestDate("2021-01", typeList).clone());
	dateList.add(new TestDate("2021-02", typeList).clone());
	dateList.add(new TestDate("2021-03", typeList).clone());
	dateList.add(new TestDate("2021-04", typeList).clone());
	dateList.add(new TestDate("2021-05", typeList).clone());
	dateList.add(new TestDate("2021-06", typeList).clone());
	dateList.add(new TestDate("2021-07", typeList).clone());
	dateList.add(new TestDate("2021-08", typeList).clone());
	dateList.add(new TestDate("2021-09", typeList).clone());
	dateList.add(new TestDate("2021-10", typeList).clone());
	dateList.add(new TestDate("2021-11", typeList).clone());
	dateList.add(new TestDate("2021-12", typeList).clone());
	
	List nameList = new ArrayList<>();
	nameList.add(new TestName("设备1", dateList).clone());
	nameList.add(new TestName("设备2", dateList).clone());
}

──────────────────────────────────────────────────

其它解决办法

循环赋值时,解决指向同一个地址方法;使用google的gson包


	com.google.code.gson
	gson
	2.8.6

@Data
class Equipment {
    String name;
    String date;
    String type;
}
Gson gson = new Gson();
Equipment equipment = new Equipment();
equipment.setName("设备1");
equipment.setDate("2021-10-28");
equipment.setType("摄像头");
List equipmentList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
	equipmentList.add(gson.fromJson(gson.toJson(equipment), equipment.getClass()));
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/354121.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号