0
package com.snapfires.pdf;

public class FultonObj {
    private String fileNo;
    private String parcelId;
    private String situs;


    public String getFileNo() {
        return fileNo;
    }
    public void setFileNo(String fileNo) {
        this.fileNo = fileNo;
    }
    public String getParcelId() {
        return parcelId;
    }
    public void setParcelId(String parcelId) {
        this.parcelId = parcelId;
    }
    public String getSitus() {
        return situs;
    }
    public void setSitus(String situs) {
        this.situs = situs;
    }

}



package com.snapfires.pdf;

import java.util.ArrayList;

public class List {

    public static void main(String arg[]) {

    ArrayList<FultonObj> list = new ArrayList<FultonObj>(); 
    FultonObj fuOb = new FultonObj();

    String file1 = "file#1";
    String file2 = "file#2";
    String file3 = "file#3";
    String parcelId1 = "parcelId1";
    String parcelId2 = "parcelId2";
    String parcelId3 = "parcelId3";
    String situs1 = "situs1";
    String situs2 = "situs2";
    String situs3 = "situs3";

        fuOb.setFileNo(file1);
        fuOb.setParcelId(parcelId1);
        fuOb.setSitus(situs1);
        list.add(fuOb);

        fuOb.setFileNo(file2);
        fuOb.setParcelId(parcelId2);
        fuOb.setSitus(situs2);
        list.add(fuOb);

        fuOb.setFileNo(file3);
        fuOb.setParcelId(parcelId3);
        fuOb.setSitus(situs3);
        list.add(fuOb);

    for(int b=0; b<list.size(); b++) {
        System.out.println(list.get(b).getFileNo());
        System.out.println(list.get(b).getParcelId());
        System.out.println(list.get(b).getSitus());
    }   

    }

}

When I run this small program, I am only getting the last object that was added to the List. Although it iterates through the list but the result is that it prints out the last object that was added. e.g.

file#3
parcelId3
situs3
file#3
parcelId3
situs3
file#3
parcelId3
situs3

I want all the objects that were added to the List to be there not just the last object. Could someone let me know why this is not happening?

I don't think creating new FultonObj object each time will solve my issue. Here is why. I am reading a text file where it is possible that one line may have only file# and second line may only have file# and parcelId and third line may have file#, parcelId, situs. Or any combination of file#, parcelId, situs per line. However the order will always be same, meaning you will always have: file#, parcelId, situs. So I need to make sure that fuOb is complete before I add it to ArrayList. If I create multiple fuOb then it is possible that none of the fuOb will be complete, meaning it has file#, parcelId, situs.

  • 2
    You're adding a reference to the same object – GBlodgett Jan 18 '19 at 05:13
  • create new `FultonObj` object everytime you are setting data. like FultonObj fuOb1 = new FultonObj(); fuOb.setFileNo(file1); fuOb.setParcelId(parcelId1); fuOb.setSitus(situs1); list.add(fuOb1); FultonObj fuOb2 = new FultonObj(); fuOb.setFileNo(file2); fuOb.setParcelId(parcelId2); fuOb.setSitus(situs2); list.add(fuOb2); – Doctor Who Jan 18 '19 at 05:16

2 Answers2

2

Your program should be like this. Everytime you are setting data in fuOb it is overriding the previous data..

FultonObj fuOb1 = new FultonObj(); 
fuOb1.setFileNo(file1); 
fuOb1.setParcelId(parcelId1); 
fuOb1.setSitus(situs1); 
list.add(fuOb1); 

FultonObj fuOb2 = new FultonObj(); 
fuOb2.setFileNo(file2); 
fuOb2.setParcelId(parcelId2); 
fuOb2.setSitus(situs2); 
list.add(fuOb2);
Doctor Who
  • 747
  • 1
  • 5
  • 28
1

make three FultonObj not use one

    fuOb1.setFileNo(file1);
    fuOb1.setParcelId(parcelId1);
    fuOb1.setSitus(situs1);
    list.add(fuOb1);

    fuOb2.setFileNo(file2);
    fuOb2.setParcelId(parcelId2);
    fuOb2.setSitus(situs2);
    list.add(fuOb2);

    fuOb3.setFileNo(file3);
    fuOb3.setParcelId(parcelId3);
    fuOb3.setSitus(situs3);
    list.add(fuOb3);
TongChen
  • 1,414
  • 1
  • 11
  • 21
  • I resolved this issue by creating new FultonObj and move the content of the another FultonObj into the newly created FultonObj and then add this new FultonObj to the ArrayList. This will prevent adding only the last FultonObj object to ArrayList. – Sanjeev Jan 19 '19 at 06:11