am trying to implement the State Space of the game towers of hanoi the problem i cannot implement a deep copy to an object the results are very wrong :) and am in a hell miss , all am trying to do is to insert the potential changes between bigs into the List

State copy = new State(v.big1,v.big2,v.big3);

I think that the issue is here , that copy still referencing to v object and alter it so the second step cannot so the intial or the original value it sees the altered value

so how do i have a new Copy of independent object rather than the object that i selected from the list

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Stack;


public class State {

    Stack big1 = new Stack();
    Stack big2 = new Stack();
    Stack big3 = new Stack();

    State(){}

    State(Stack big1,Stack big2,Stack big3){

        this.big1=big1;
        this.big2=big2;
        this.big3=big3;

           }


    @Override
    public String toString()
    {
        System.out.println(Arrays.toString(this.big1.toArray())+Arrays.toString(this.big2.toArray())+Arrays.toString(this.big3.toArray())+',');
        return "fnish";
    }
}



import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;


public class Play{

List<State> statespace = new CopyOnWriteArrayList<State>();
int levelcount = 0;
int nodeNum;



public boolean checkpeek(int a , Stack s)
{
    if(s.isEmpty())
    {
       // System.out.println("empty stack");//----stck 1
     return true;
    } 
    else {
         //System.out.println("not empty stack");//----stck 1
           if(a<(Integer)s.peek())
           {
              //  System.out.println("not empty stack and OK");//----stck 1
             return true;
           } else {
             // System.out.println("fasle condation");//----stck 1
                   return false;
                  }
        }
}

public void breadthMove(State i,State g)
{
    int count = 0;
    int tempint= 0;

    statespace.add(i);
    //Iterator it = statespace.iterator();
    //while (it.hasNext())
    for(State v : this.statespace)
    {
      if(count==100)
      {
        break;
      }
      State copy = new State(v.big1,v.big2,v.big3);
      copy.toString();
      v.toString();
      /*
       insert from big1 to big2 and big3
       */
      if(!copy.big1.isEmpty())
      {
        tempint = (Integer)copy.big1.pop();
        //System.out.println(tempint);
        if(copy.big2.isEmpty() || (checkpeek(tempint,copy.big2)))
        {
           copy.big2.push(tempint);
           System.out.println(copy.toString());
           if(!checkExistance(copy,g))
           {
                statespace.add(copy);
                count++;
                this.nodeNum=count;
                copy = null;
                //copy.toString();
                copy = new State(v.big1,v.big2,v.big3);
                copy.toString();
                System.out.println("--------------------------copy1");
                v.toString();
                System.out.println("--------------------------v");
           }
        }
      }

      if(!copy.big1.isEmpty())
      {
        System.out.println("--------------------------v2");
        tempint = (Integer)copy.big1.pop();
        if(copy.big3.isEmpty() || (checkpeek(tempint,copy.big3)))
        {
           copy.big3.push(tempint);
           if(!checkExistance(copy,g))
           {
                statespace.add(copy);
                count++;
                this.nodeNum=count;
                copy = null;
                copy = new State(v.big1,v.big2,v.big3);
           }
        }
      }
      /*
        End insert from big1 to big2 and big3
      */
      /*
        insert from big2 to big1 and big3
      */
      if(!copy.big2.isEmpty())
      {
        tempint = (Integer)copy.big2.pop();
        if(copy.big1.isEmpty() || (checkpeek(tempint,copy.big1)))
        {
           copy.big1.push(tempint);
           if(!checkExistance(copy,g))
           {
                statespace.add(copy);
                count++;
                this.nodeNum=count;
                copy = null;
                copy = new State(v.big1,v.big2,v.big3);
           }
        }
      }

      if(!copy.big2.isEmpty())
      {
        tempint = (Integer)copy.big2.pop();
        if(copy.big3.isEmpty() || (checkpeek(tempint,copy.big3)))
        {
           copy.big3.push(tempint);
           if(!checkExistance(copy,g))
           {
                statespace.add(copy);
                count++;
                this.nodeNum=count;
                copy = null;
                copy = new State(v.big1,v.big2,v.big3);
           }
        }
      }
      /*
        End insert from big2 to big1 and big3
      */

      /*
        insert from big3 to big1 and big2
      */
      if(!copy.big3.isEmpty())
      {
        tempint = (Integer)copy.big3.pop();
        if(copy.big1.isEmpty() || (checkpeek(tempint,copy.big1)))
        {
           copy.big1.push(tempint);
           if(!checkExistance(copy,g))
           {
                statespace.add(copy);
                count++;
                this.nodeNum=count;
                copy = null;
                copy = new State(v.big1,v.big2,v.big3);
           }
        }
      }

      if(!copy.big3.isEmpty())
      {
        tempint = (Integer)copy.big3.pop();
        if(copy.big2.isEmpty() || (checkpeek(tempint,copy.big2)))
        {
           copy.big2.push(tempint);
           if(!checkExistance(copy,g))
           {
                statespace.add(copy);
                count++;
                this.nodeNum=count;
                copy = null;
                copy = new State(v.big1,v.big2,v.big3);
           }
        }
      }
      /*
        End insert from big3 to big1 and big2
      */
    }
}

// Check stack equality and chaeck if it's not goal
public boolean checkExistance (State s , State g)
{
    boolean result =false;
    for (State v : this.statespace)
    {
        if((compareStacks(v.big1,s.big1))
        && (compareStacks(v.big2,s.big2)))
        {
            if (compareStacks(s.big3,g.big3))
            {
                return result = false;
            }else {
                    return result = true;
                  }
        }
    }
    return result;
}

public static boolean compareStacks(Stack a,Stack b)
{
  boolean result;
  if (a.isEmpty() != b.isEmpty())
  {
      return result=false;
  }
  if (a.isEmpty() && b.isEmpty())
  {
      return result=true;
  }

int x;
int y;
     if(a.isEmpty()){
            x = 0;
            }else{
             x = (Integer)a.pop();
            }
        if(b.isEmpty()){
            y = 0;
            }else{
            y = (Integer)b.pop();
            }


  if (((x==0)
    && (y!=0))
    || x != y)
    {
    a.push(x);
    b.push(y);
    return result=false;
    }

  result = compareStacks(a,b);
  a.push(x);
  b.push(y);
  return result;
}


public void print()
{
  for(State v : this.statespace)
  {
    v.toString();
  }
}

public static void main(String[] args) {

        State intial = new State();
        State goal   = new State();

        intial.big1.push(3);
        intial.big1.push(2);
        intial.big1.push(1);

        goal.big3.push(3);
        goal.big3.push(2);
        goal.big3.push(1);

        Play p = new Play();
        p.statespace.clear();
        p.breadthMove(intial, goal);
        p.print();
    }
}

You want what's called a "deep copy". Google that for examples of how to do it, and discussions about why it's a pain in the * and what the alternatives are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.