How is it possible my array is broken?

Posted by user1812765 on Stack Overflow See other posts from Stack Overflow or by user1812765
Published on 2012-12-14T10:56:14Z Indexed on 2012/12/14 11:04 UTC
Read the original article Hit count: 138

Filed under:
|

I have this piece of code:

public lot merge (lot otherlot){
    wafer[] mWaferarray = new wafer[16];
    byte[] bytearray = new byte[16];
    wafer resultwafer = new wafer(bytearray);
    wafer w1;
    wafer w2;
    int i;
    int[][] assignmentmatrix = HungarianAlgorithm.computeAssignments(convertinttofloat (solutionmatrix(otherlot)));

    for (i=0; i != assignmentmatrix.length ;i++){

        w1 = otherlot.getWaferarray()[assignmentmatrix[i][0]];

        w2 = getWaferarray()[assignmentmatrix[i][1]];


        resultwafer.setWafer(w1.wafercompare(w2));

        mWaferarray[i] = resultwafer;
        mWaferarray[i].print();



    }
    System.out.println("HERE\n");
    mWaferarray[5].toString();

    resultlot = new lot(mWaferarray);
    resultlot.print();// Problem occurs here.

    return resultlot;

}

As you can see I create an array of wafers (selfdefined class). Then I fill this up with new wafers. When I print this array (mWaferarray[i].print()) it gives me the wanted results. But when I go out of the "for"-loop the array is broken and it is as if the last item I add to mWaferarray fills it up (the entire array, 16 long, is filled with this wafer). So if run this program this is what I get:

1011110010111100
0011011111111110
0111110111101101
1010111001101111
0110110111101111
1010110101111010
1010110111011110
1011111010111100
1111110011101110
0111111111011011
1111111111011010
1101111011111010
1010110101011110
0101111011011010
1011111011011000
0101111011011010
HERE

0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010
0101111011011010

As you can see it is as if the array is filled with the last wafer.

I have been looking at this for some time now, I hope you guy can help me out.

Thx in advance

PS: my print functions are written like this:

    void print(){
    int j;
    for (j=0; j != waferarray.length ;j++){
        waferarray[j].print();

        }


    }

EDIT: added code for lot

this is the beginning of the lot class

public class lot {


wafer[] waferarray = new wafer[16];
lot resultlot;

public lot (wafer wafer1,wafer wafer2,wafer wafer3,wafer wafer4,
        wafer wafer5,wafer wafer6,wafer wafer7,wafer wafer8,
        wafer wafer9,wafer wafer10,wafer wafer11,wafer wafer12,
        wafer wafer13,wafer wafer14,wafer wafer15,wafer wafer16){

    waferarray[0] = wafer1;
    waferarray[1] = wafer2;
    waferarray[2] = wafer3;
    waferarray[3] = wafer4;
    waferarray[4] = wafer5;
    waferarray[5] = wafer6;
    waferarray[6] = wafer7;
    waferarray[7] = wafer8;
    waferarray[8] = wafer9;
    waferarray[9] = wafer10;
    waferarray[10] = wafer11;
    waferarray[11] = wafer12;
    waferarray[12] = wafer13;
    waferarray[13] = wafer14;
    waferarray[14] = wafer15;
    waferarray[15] = wafer16;


}

public lot (wafer[] thiswaferarray){

    waferarray = thiswaferarray;


}

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays