Deep Copy for an array of strings

911 Views Asked by At

I need to demonstrate a code which implements deep copy for an array of strings in JAVA . Following is the code I developed.Anybody can confirm if it is correct or not?

    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;
    }
}
0

There are 0 best solutions below