-1

I've been trying to tackle a programming-problem,and have come across a scoping problem. Here is a SCE of the problem:

for (int Hole1 = 1; Hole1 < beavers; Hole1++) {
        list = exampleList;

        for (int t = 0; t < Hole1; t++) {
            temp.add(list.get(0));
            list.remove(0);
        }

        for (int p = 0; p < Hole1; p++) {
            list.add(temp.get(temp.size() - 1));
            temp.remove(temp.size() - 1);
        }
        exampleList2 = list;

        for (int Hole2 = 1; Hole2 < beavers; Hole2++) {
            list = exampleList2;

            for (int t = 0; t < Hole2; t++) {
                temp.add(list.get(0));
                list.remove(0);
            }

            for (int p = 0; p < Hole2; p++) {
                list.add(temp.get(temp.size() - 1));
                temp.remove(temp.size() - 1);
            }
            exampleList3 = list;

            for (int Hole3 = 1; Hole3 < beavers; Hole3++) {
                list = exampleList3;

                for (int t = 0; t < Hole3; t++) {
                    temp.add(list.get(0));
                    list.remove(0);
                }

                for (int p = 0; p < Hole3; p++) {
                    list.add(temp.get(temp.size() - 1));
                    temp.remove(temp.size() - 1);
                }

                System.out.println(list.toString() + " " + Hole1 + " " + Hole2 + " " + Hole3);
                if (check(list))
                    System.out.println("match");
            }
        }
    }

My problem is that examplelist3 changes value for every iteration of the hole3 for loop, but from my understanding each loop completes it's iterations before moving on. Anyone have an idea of what's going on?

EDIT* removed the SCE and posted the code instead

AlexVestin
  • 2,548
  • 2
  • 16
  • 20
  • Maybe you can add what you are trying to achieve? It seems like you are doing some nested iterations/assignments in some awkward way. – randers Oct 13 '15 at 10:32
  • `each for-loop completes it's iterations before moving on.` That is what happens actually. – Suresh Atta Oct 13 '15 at 10:33
  • Well, it's a a problem from a competition I couldn't crack. You start with a list of 5 4 3 2 1, running them through 3 "holes" which are between 1 and n-1 deep. [the problem can be seen here on page 5](http://www.progolymp.se/static/arkiv/kval15.pdf). Then to match them in a certain ortder and get the hole depths. – AlexVestin Oct 13 '15 at 10:40

2 Answers2

1

Going by the code in the question, all the variables - list, startlist, examplelist2 and examplelist3 - refer to the same list object. This means that if you make any changes to any of these variables, all other variables will show the updated list since they all reference the same object.

Thus even though the code updates list in the 3rd for loop, the changes will also be reflected in exampleList3 since both variables point to the same object in the heap.

Check this SO question.

Community
  • 1
  • 1
nhylated
  • 1,575
  • 13
  • 19
0

from my understanding each for-loop completes it's iterations before moving on.

Thats true.

What is happening in your code? Iteration of for(hole2) is being done complete, which means that in each iteration will execute for(hole3), because is inside the scope of for(hole2)

Each iteration of for(hole2) will always be this code:

list = examplelist2;
//sorting list
examplelist3 = list;

for(hole3){
    list = examplelist3;
    //sorting list
    //check if matching 
}   

What implies to iterate over for(hole3) each time.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109