0

Im not sure why my nested for loop is not going through the if statements more then once. After the initial run through, I am trying to make the copy of the new array become the one that goes through the if statements and draws on a new line. Area is being subtracted each time, however nothing is being drawn which means the if statements are not being run. Not sure why not.

   public static int [] completion(int [] updated, int length, double width)
    {
        int [] second = new int [length];
        double ending = 49;
        double endingY = 20;
        double x = 1;
        double y = 20;
        double begin = 2;
        double area = 20;
        for(int k = 0; k < 20; k++){ 
            for(int i = 1; i < 49 - 1; i++) {
                int [] done = updated;
                if(0 == done[i - 1] && 0 == done[i] && 0 == done[i + 1]){
                    StdDraw.square(begin,area,width);
                    begin = begin + 1;
                    second[i] = 0;
                }
                else if (1 == done[i - 1] && 1 == done[i] && + 
                         1 == done[i + 1]){
                    StdDraw.square(begin,area,width);
                    begin = begin + 1;
                    second[i] = 0; 
                }
                else {
                    StdDraw.filledSquare(begin, area, width);
                    begin = begin + 1;
                    second[i] = 1;
                }

            }
            updated = second.clone();
            area = area - 1;
            StdDraw.square(x,y,width);
            StdDraw.square(ending,endingY,width);
            y = y-1;
            updated[0] = 0;
            updated[48] =0;
            endingY = endingY - 1;





        }
        return updated;
    }
Craig S. Anderson
  • 6,966
  • 4
  • 33
  • 46
  • 2
    What does debugging tell you? – Thorsten Dittmar Apr 10 '15 at 08:09
  • 2
    Is `int [] done = updated` supposed to copy the contents of the `updated` array? It doesn't. – dhke Apr 10 '15 at 08:11
  • I am in an introductory course at my college, so i dont really know how to debug. Im just really confused on why the if statements wont draw lines a second time. – Brian Bjurstrom Apr 10 '15 at 08:15
  • does int [] done = was supposed to copy the contents of updated. so it is supposed to write int [] done = updated.clone(); – Brian Bjurstrom Apr 10 '15 at 08:18
  • Just a tip @BrianBjurstrom - now, as a complete beginner, is the time to learn to us a debugger. It will save you hours. If you're not already using an IDE like Eclipse, do. The debugger is only a click away. – slim Apr 10 '15 at 09:23

1 Answers1

0

See this. Cloning an array does not clone the content. It just creates a new empty array of same type and length.

So you need to change updated = second.clone(); to updated = second;, if I'm correctly guessing your intent.

Community
  • 1
  • 1
Sibbo
  • 3,796
  • 2
  • 23
  • 41
  • So After correcty copying the arrays, shouldn't the contents of second which is now in the array done, be put through the if statements and then printed? By adjusting area by subtracting 1, I would assume that it would draw directly under the first drawing, however my program is still failing to go through the if statements a second time. – Brian Bjurstrom Apr 10 '15 at 08:47