简体   繁体   English

由于某些原因连接了2个分离的对象

[英]2 separated objects are connected for some reason

This is my homework(assignment). 这是我的作业(作业)。 I really hope someone can help me figure out what I did wrong. 我真的希望有人能帮助我找出我做错了什么。

When I tried to create an object of State state = new State(tablesize, tb); 当我尝试创建State对象时state = new State(tablesize,tb);

and then tried to make a copy State st2 = new State(state); 然后尝试复制一个状态st2 = new State(state);

and then tried to modify the data in state state.placeNumber(1,1,3); 然后尝试修改状态为state.placeNumber(1,1,3);的数据。

for some reason, the data in st2 is also changed. 由于某些原因,st2中的数据也会更改。

Below is the code. 下面是代码。 I really hope someone can point out where my mistake is. 我真的希望有人能指出我的错误所在。

Thanks 谢谢

public class State 
{

private int arraysize;
private int lastfilledx;
private int lastfilledy;
private int table[][];

//constructor

    public State()
    {

    }
    public State(State s)
    {
        arraysize = s.getsize();
        lastfilledx = s.lastindex_x();
        lastfilledy = s.lastindex_y();
        table = s.gettable();
    }
    public State(int size, int[][] tb)
    {
        arraysize = size;
        table = new int[size][size];        
        //copy the initial table which is a 2d array
        table = tb;     
        for (int i = 0 ; i < size; i++)
        {
            for(int j = 0 ; j < size ; j++)
            {
                 if ( table[i][j] == 1)
                 {

                    lastfilledx = i; 
                    lastfilledy =j;                   
                    break;
                 }
            }
        }
    }

public void placeNumber(int i, int j, int nextvalue)
{
    lastfilledx = i; 
    lastfilledy = j;    
    table[i][j] = nextvalue;
}   

public void printoutput()
{
        for (int i=0; i < arraysize; i++)
        {
            for (int j=0; j < arraysize; j++)
                System.out.print("  " + table[i][j]);

            System.out.println("");
        }

        System.out.println("last fill " + lastfilledx + " " + lastfilledy);
}

public int[][] gettable()
{
    return table;
}

public int getsize()
{
    return arraysize;
}
public int lastindex_x()
{
    return lastfilledx;
}
public int lastindex_y()
{
    return lastfilledy;
}
}




public class Dikuho extends State
{
    private static State state;

public static void main(String[] args) {

    int tablesize = 3;
    int tb[][] = new int[tablesize][tablesize];

    /*****HARDCODE the table data***/
    for (int i=0; i < tablesize; i++)
        for (int j=0; j < tablesize; j++)
            tb[i][j] = 0;


    //test case for 3x3
    tb[2][2] = 1;
    tb[0][0] = tablesize*tablesize;
    tb[0][1] = 7;
    tb[1][0] = 8;
    tb[2][1] = 2;       

    //initialize the state
    state = new State(tablesize, tb);


    **//Here is where the problem is. I only change the data in state but the data in st2 is also changed. I'm not sure what happen here.**
    State st2 = new State(state);   
    state.placeNumber(1,1,3);

    st2.printoutput();    **//These both printout same output which is not correct**    
    state.printoutput();
    }


}

Your copy constructor has made a shallow copy of table 2D array. 您的副本构造函数已对table 2D数组进行了浅表复制 Both the original object and the copy refer to the same original array, because you assign the array reference from the original to the new object. 原始对象和副本都引用相同的原始数组,因为您将数组引用从原始对象分配给了新对象。 That's fine for the int values, because the values are copied. 这对于int值很好,因为这些值是复制的。 But that's not okay for objects, for which references to the obejct are copied. 但这不适用于复制对象引用的对象。

Instead of just copying the reference to the array... 不仅仅是将引用复制到数组中...

table = s.gettable();

You'll need to create a new, copied array: 您需要创建一个新的复制数组:

table = new int[arraysize][arraysize];
// 2 nested for loops here to copy the contents
public int[][] gettable()
{
    return table;
}

So, both of your state objects are referencing the same array. 因此,两个状态对象都引用同一数组。 you need to create a new array for each instance. 您需要为每个实例创建一个新数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM