在 Java 中复制向量中的向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11414027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:00:11  来源:igfitidea点击:

Copy Vector in a Vector in Java

javacollections

提问by researcher

I'd like to copy a vector to another. The problem is that if I change the vector v1, the second vector v2is changing too. My goal is to keep the copy intact even if I change the source vector.

我想将一个向量复制到另一个向量。问题是,如果我改变 vector v1,第二个 vectorv2也会改变。我的目标是即使更改源向量也能保持副本完整。

import java.util.Collections;
import java.util.Vector;

public class CopyElementsOfVectorToVectorExample {

  public static void main(String[] args) {

    //create first Vector object
    Vector v1 = new Vector();

    //Add elements to Vector
    v1.add("1");
    v1.add("2");
    v1.add("3");

    //create another Vector object
    Vector v2 = new Vector(v1.size());  

    v2.setSize(v1.size());
    Collections.copy(v2,v1);


    System.out.println("After copy, Second Vector Contains : " + v2);  
  }}

How can I keep the second copy intact?

我怎样才能保持第二个副本完好无损?

回答by JB Nizet

Your code above is fine, although too complex. It could be reduced to

你上面的代码很好,虽然太复杂了。可以减少到

Vector copy = new Vector(original);

Also, you should avoid Vector, and use ArrayList instead.

此外,您应该避免使用 Vector,而使用 ArrayList。

回答by aioobe

[...] I change the vector v1, the second v2 is changing too ...My goal is to keep the copy intact even if I change the source vector ..

[...]我改变了向量 v1,第二个 v2 也在改变......我的目标是即使我改变源向量也能保持副本完整......

The vector contains referencesto objects. If you change the objects, then the change will be visible from both vectors.

该向量包含对对象的引用。如果您更改对象,则更改将从两个向量中都可见。

I believe what you're after is a deep copy.

我相信你所追求的是深拷贝

回答by KingMaker

You can use Below code to clone a vector.

您可以使用下面的代码来克隆一个向量。

Vector v1 = new Vector();

//Add elements to Vector
v1.add("1");
v1.add("2");
v1.add("3");

//create another Vector object
Vector v2 = (Vector)v1.clone();  

回答by amicngh

the problem is if I change the vector v1, the second v2 is changing too ...My goal is to keep the copy intact even if I change the source vector

the problem is if I change the vector v1, the second v2 is changing too ...My goal is to keep the copy intact even if I change the source vector

This is because Collections.copy(v2,v1)make a sallow copynot deep copy.

这是因为Collections.copy(v2,v1)赚了蜡黄复制深拷贝

Make deep copyof your Vector.

深复制你的Vector

Apologize for previous answer.

为之前的回答道歉。

Edit:

编辑:

I am assuming that your vectoris containing objects of type Serializable. With this approach you can get a deep copy of your collection.

我假设您vector包含类型为 的对象Serializable。通过这种方法,您可以获得收藏的深层副本。

static public Object deepCopy(Object oldObj) throws Exception {
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(); // A
        oos = new ObjectOutputStream(bos); // B
        // serialize and pass the object
        oos.writeObject(oldObj); // C
        oos.flush(); // D
        ByteArrayInputStream bin = new ByteArrayInputStream(
                bos.toByteArray()); // E
        ois = new ObjectInputStream(bin); // F
        // return the new object
        return ois.readObject(); // G
    } catch (Exception e) {
        System.out.println("Exception in ObjectCloner = " + e);
        throw (e);
    } finally {
        oos.close();
        ois.close();
    }
}