简体   繁体   中英

How Can Copy from one Arraylist to another

How Can Copy from one Arraylist to another like Below. I have Arraylist of below class like

List<A> aClass =...; // has some data

List<B> bClass =...; //empty

//imports Class X
public class A {
    private String p;
    private String q;
    private<ClassX> x
}


//imports Class Y
public class B {
    private String p;
    private String q;
    private<ClassY> y
}

public class X {
    private String r;
    private String s;
}

 public Class Y {

     private String r;
     private String s;

}

How Can i Copy from bClass to aClass ; Like below //bClass=aClass;

You can use for loop for that:

for(A a : aClass) {
    bClass.add(new B(a.getP(), a.getQ())); // creating new instance of B by passing params to its constructor.
}

public class A {
    private String p;
    private String q;
    
    public String getP() {
        return p;
    }

    public String getQ() {
        return q;
    }
}

public class B {
    private String p;
    private String q;

    public B(String p, String q) {
        this.p = p;
        this.q = q;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM