首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java对象克隆问题

Java对象克隆问题
EN

Stack Overflow用户
提问于 2011-04-20 07:31:04
回答 2查看 409关注 0票数 1

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

代码语言:javascript
复制
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数组与子节点的数组变得相同。我试图克隆节点对象,但它没有修复它。如果有人有一个想法如何解决它并分享它,我将不胜感激。

代码语言:javascript
复制
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“消息。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-04-20 07:42:58

克隆机制没有任何损坏或错误。只有一群或程序员不理解它,这里有一个适合你的类的有效方法。

代码语言:javascript
复制
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。

票数 3
EN

Stack Overflow用户

发布于 2011-04-20 07:40:35

您可能希望查看Object的documentation ()方法的克隆。默认的克隆实现(如果您实现了Cloneable )执行浅拷贝。这可能不是您想要的,您可以编写自己的复制方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5723939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档