search
HomeJavaJavagetting StartedJava implements copying list

Java implements copying list

Sep 11, 2020 pm 03:51 PM
javalist

Java implements copying list

Background:

At first I wanted to copy a collection when entering a new page and save it for subsequent operations, so I just wrote List A=List B, later I found that when operating B, the data in A will also change accordingly.

(Video tutorial recommendation: java course)

It was found through query that directly using "=" is equivalent to an array with the same content in java pointing to the same address, that is After shallow copying, A and B point to the same address. The consequence is that changing B will also change A, because changing B means changing the content of the address pointed to by B. Since A also points to the same address, A and B are changed together. To copy an independent array, you can use the following method:

public class GoodsBean extends HttpResult implements Cloneable{
@Override
protected Object clone() throws CloneNotSupportedException {
    GoodsBean o = null;
    try {
        o = (GoodsBean) super.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    return o;
}
public  ArrayList<GoodsBean> deep_clone(ArrayList<GoodsBean> cells, ArrayList<GoodsBean> clone_cells){
    for(GoodsBean c : cells){
        try {
            clone_cells.add((GoodsBean) c.clone());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
    return clone_cells;
}
}

Related recommendations: java introductory tutorial

The above is the detailed content of Java implements copying list. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.