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]
```