0

我需要演示一个在 JAVA 中为字符串数组实现深拷贝的代码。以下是我开发的代码。任何人都可以确认它是否正确?

    public class Paper implements Cloneable{

private String Type ; 
private String[] Text ; 

//Default constructor to initialize variables.
public Paper()
{ 
        this.Type="" ; 
    this.Text = new String[0]  ;  
}

//Necessary constructor.
public Paper(String Type, String[] Text)
{
this.Type= Type;
//Deep copy 
this.Text = new String[Text.length] ; 
this.Text = Text.clone() ;
//Flat Copy
this.Text = Text ; 
}

//Here we implements the interface "Cloneable" to make deep copy when we want to 
//extend an array of Paper objects.
public Object clone()
{
    try
    {
        Paper cloned = (Paper) super.clone();
        cloned.Text = (String[])Text.clone();
        return cloned;
    }
    catch(CloneNotSupportedException e)
    {
        return null;
    }
}
4

0 回答 0