0

I am creating an Object inside a for loop and adding it to a list as illustrated in the code below. The values are distinct and different when logged inside the for loop how ever once the loop ends, and I reprint the list I see that all the values are the same. Not sure whats going wrong in the code

private class PageTablesAreas{
        Page page;
        List<Rectangle> areas;
        public PageTablesAreas(Page page,List<Rectangle> areas){
            this.page = page;
            this.areas = areas;
        }

        public Page page(){
            return this.page;
        }

        public int getPageNumber(){
            return this.page.getPageNumber();
        }

        public List<Rectangle> areas(){
            return this.areas;
        }
    }

List<PageTablesAreas> pageTableAreas = new ArrayList<>();
        for(int i=FIRST_PAGES.length+1;i<statementLength-LAST_PAGES.length;i++){
            Page p = statement.getPage(i);
            List<Rectangle> pareas = new ArrayList<>();
            for(Rectangle area: gPageAreas.areas()){
                pareas.add(fineTuneArea(p,area));
            }
            pageTableAreas.add(new PageTablesAreas(p,pareas));
            System.out.println("Inside loop value" + pageTableAreas.get(pageTableAreas.size()-1).areas().get(0));
        }

        System.out.println("Outside loop");
        for(PageTablesAreas pta : pageTableAreas){
            System.out.println("Outside loop value" + pta.areas().get(0));
        }

```

Carefully observe bottom =
```
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.01,bottom=516.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=370.0,bottom=528.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=362.01,bottom=520.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=372.0,bottom=530.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=371.01,bottom=529.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=357.01,bottom=515.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=378.0,bottom=536.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=352.01,bottom=510.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=378.0,bottom=536.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=372.01,bottom=530.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.01,bottom=516.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=378.0,bottom=536.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=372.01,bottom=530.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=378.0,bottom=536.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.01,bottom=516.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=362.0,bottom=520.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=352.01,bottom=510.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=368.0,bottom=526.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=378.0,bottom=536.000000,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=370.01,bottom=528.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=354.01,bottom=512.010010,right=808.000000]
Inside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
Outside loop valuetechnology.tabula.Rectangle[x=71.0,y=158.0,w=737.0,h=358.0,bottom=516.000000,right=808.000000]
```
  • Since your inner loop only prints the last added item you don't notice that this item "overwrites" all previous items. You can check that by printing the first item in the loop. It should print the same item again and again (since it is always the first and shouldn't change), but it will yield the same result as in your current output, because the first item gets modified. What ever object `fineTuneArea` returns, its properties might be `static` – Tom Sep 02 '19 at 20:20

1 Answers1

1

Rectancle is an object. If you modify Rectangle then you modify one single instance of it because you pass the reference to the function and not a copy of the "value" like you would with int for example.

There is no new Rectangle(...) in your code and that is why I assume you do not create a new Rectangle in fineTuneArea(...).

For example if you have something like this:

Rectangle r = new Rectangle(10, 10);
modifyWidthLenghtTo100(r);

System.out.println(r.getWidth()) will print 100 

The inner loop print works because you print the result and in the next pass you overwrite the last result and print the actual result. In the outer loop you print every result and see the final result of every operation.

Tom's comment is also correct. If you have some static variables they exists once for every Rectangle you create.

Charlie
  • 1,169
  • 2
  • 16
  • 31