开发者

Java obj cloning problem

I would need some help with the following code if you are kind. Basically i have a tree node that remembers it's parent node, depth-level and his current state(a 2D array). Most variable names are written in my native language, i hope this is not a problem :

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();
    b开发者_C百科oolean 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;
}

}

Now, i create an initial node and after, a child-node from it. The problem is that when i create the child-node, the parent's 2D array becomes identical with the child's array. I tried to clone the node object but it didn't fix it. I would appreciate if someone has an ideea how to fix it and shares it.

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");
}

}

So if p = {{7,2,4},{5,0,6},{8,3,1}} the if statement should return "not ok", but instead i get the "ok" message.


There is nothing broken or wrong about the clone mechanism. Just a bunch or programmers who don't understand it, here's a valid one for you class.

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");
    }
}

This implementation assumes that the clone wants to use the same parent as the original. If this is not the case, you will need to clone that as well, or perhaps set the parent to null.


You may want to look at the documentation of the clone() method of Object. Default clone implementation (that is if you implemented Cloneable, which you did) performs shallow copy. This is probably not what you want, you may be better off by writing your own copying method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜