Java obj克隆问题

时间:2011-04-19 23:31:04

标签: java

如果你很善良,我需要一些以下代码的帮助。 基本上我有一个树节点,可以记住它的父节点,深度级别和他当前的状态(2D数组)。 大多数变量名都是用我的母语编写的,我希望这不是问题:

public class Nod implements Cloneable {

private Nod parinte;//parent node
private int[][] stare;//state
private int cost;//depth-level
private String actiune;//the action used to obtain this node
private volatile int hashCode = 0;

public boolean equals(Object obj)
{
    if(this == obj)
    {
        return true;
    }
    if (!(obj instanceof Nod))
    {
        return false;
    }
    Nod nod = (Nod)obj;
    return cost == nod.getCost() && actiune.equals(nod.getActiune()) 
            && stare.equals(nod.getStareNod());
}

public int hashCode()
{
    StringBuffer strBuff = new StringBuffer();
    try 
    {
        int n = Problema.Dimensiune();//returns the dimension of state matrix
        for (int i=0;i<n;i++)
            for (int j=0;j<n;j++)
                strBuff.append(stare[i][j]);
        strBuff.append(cost);
        strBuff.append(actiune);

        String str = strBuff.toString();
        hashCode = str.hashCode();

    } 
    catch (IOException e) {
        e.printStackTrace();
    }

    return hashCode;
}   

  public Object clone() throws CloneNotSupportedException 
  {
        return super.clone();
  }


public static boolean goUp(int[][] st) throws IOException
{

    int n = Problema.Dimensiune();
    boolean ok = false;
    int[][] a = st;

    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
        {
            if (a[i][j] == 0)
                if (i != 0)
                    ok = true;
        }
    return ok;
}

public static boolean goDown(int[][] st) throws IOException
{

    int n = Problema.Dimensiune();
    boolean ok = false;
    int[][] a = st;

    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
        {
            if (a[i][j] == 0)
                if (i != (n-1))
                    ok = true;
        }
    return ok;
}

public static boolean goLeft(int[][] st) throws IOException
{

    int n = Problema.Dimensiune();
    boolean ok = false;
    int[][] a = st;

    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
        {
            if (a[i][j] == 0)
                if (j != 0)
                    ok = true;
        }
    return ok;
}

public static boolean goRight(int[][] st) throws IOException
{

    int n = Problema.Dimensiune();
    boolean ok = false;
    int[][] a = st;

    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
        {
            if (a[i][j] == 0)
                if (j != (n-1))
                    ok = true;
        }
    return ok;
}


public static int[] Zero(int[][] st) throws IOException
{

    int[][] a = st;
    int n = Problema.Dimensiune();
    int[] b = new int[2];

    for (int i=0;i<n;i++)
        for (int j=0;j<n;j++)
            if (a[i][j] == 0)
            {
                b[0] = i;
                b[1] = j;
            }

    return b;
}

public static int[][] Actiune(int[][] st, String s) throws IOException
{

    int[][] a = st;
    int[] b = Zero(st);

    if ((goRight(st) == true) && (s == "right"))
    {

        a[b[0]][b[1]] = a[b[0]][b[1]+1];
        a[b[0]][b[1]+1] = 0;
    }

    if ((goLeft(st) == true) && (s == "left"))
    {

        a[b[0]][b[1]] = a[b[0]][b[1]-1];
        a[b[0]][b[1]-1] = 0;
    }

    if ((goUp(st) == true) && (s == "up"))
    {

        a[b[0]][b[1]] = a[b[0]-1][b[1]];
        a[b[0]-1][b[1]] = 0;
    }

    if ((goDown(st) == true) && (s == "down"))
    {

        a[b[0]][b[1]] = a[b[0]+1][b[1]];
        a[b[0]+1][b[1]] = 0;
    }

    return a;
}

public Nod(){}

public Nod (int[][] st) 
{
    parinte = null;
    stare = st;
    cost = 0;
    actiune = null;
}

public Nod (Nod nod)
{
    parinte = nod.parinte;
    stare = nod.stare;
    cost = nod.cost;
    actiune = nod.actiune;
}

public Nod(Nod nodp, String ac) throws IOException
{
    this.parinte = nodp;
    this.cost = parinte.getCost()+1;
    this.actiune = ac;
    this.stare = Actiune(parinte.getStareNod(),actiune);
}

public void setCost(int cost)
{
    this.cost = cost;
}

public int getCost(){
    return this.cost;
}

public void setStareNod(int[][] stare)
{
    this.stare = stare;
}

public int[][] getStareNod(){
    return this.stare;
}

public void setNodParinte(Nod parinte)
{
    this.parinte = parinte;
}

public Nod getNodParinte() throws IOException{
    return this.parinte;
}

public void setActiune(String actiune)
{
    this.actiune = actiune;
}

public String getActiune()
{
    return this.actiune;
}

}

现在,我创建一个初始节点,然后创建一个子节点。问题是当我创建子节点时,父节点的2D数组变得与子节点相同。我试图克隆节点对象但它没有修复它。如果有人有一个想法如何解决它并分享它,我将不胜感激。

public class test {

public static void main(String[] args) throws IOException, CloneNotSupportedException
{   
    int[][] p = Problema.stareInitiala();
    Nod nod = new Nod(p);

    Nod nodc = (Nod) nod.clone();
    Nod nod1 = new Nod(nodc,"right");

    Nod nod1c = (Nod) nod1.clone();
    Nod nod2 = new Nod(nod1c,"up");

    if (nod.getStareNod().equals(nod.getStareNod()))
        System.out.print("ok");
    else 
        System.out.print("not ok");
}

}

所以如果p = {{7,2,4},{5,0,6},{8,3,1}} if语句应该返回“not ok”,而是我得到“ok”消息。

2 个答案:

答案 0 :(得分:3)

克隆机制没有任何破坏或错误。只是一群或不了解它的程序员,这里有一个适合你的课程。

public Object clone() {
    try {
        Nod clone = (Nod)super.clone();
        clone.stare = (int[][])stare.clone();
        return clone;
    } catch (CloneNotSupportedException cnse) {
       //won't happen;
       throw new RuntimeError("Won't happen");
    }
}

此实现假定克隆要使用与原始父级相同的父级。如果不是这种情况,您还需要克隆它,或者将父项设置为null。

答案 1 :(得分:0)

您可能希望查看Object的clone()方法的documentation。默认克隆实现(即,如果您实现了Cloneable,则执行)执行浅拷贝。这可能不是你想要的,你可以通过编写自己的复制方法来获得更好的效果。