简体   繁体   中英

Adding object to an array list in Java

I am trying to add an object to an array list and have the following -

public static void main(String[] args) {

        authors Authors = new authors();
        ArrayList<authors> tabAuthors = new ArrayList<authors>();

        Authors.setAuthId(1);
        Authors.setAuthName("Roald Dahl");
        System.out.println(Authors.toString());
        tabAuthors.add(Authors);


        Authors.setAuthId(2);
        Authors.setAuthName("Julia Donaldson");
        System.out.println(Authors.toString());
        tabAuthors.add(Authors);

        for (int counter =0; counter < tabAuthors.size(); counter++) {
        System.out.println(tabAuthors.get(counter).getAuthId() + " " + tabAuthors.get(counter).getAuthName() );
        }
   }
}

The authors class

public class authors {

    private int authId;
    private String authName;


    public int getAuthId() {
        return authId;
    }

    public void setAuthId(int authId) {
        this.authId = authId;
    }

    public String getAuthName() {
        return authName;
    }

    public void setAuthName(String authName) {
        this.authName = authName;
    }

    @Override
    public String toString() {
        return "authors{" +
                "authId=" + authId +
                ", authName='" + authName + '\'' +
                '}';
    }
}

I was expecting the code to return - 1 Roald Dahl 2 Julia Donaldson

Instead, I am getting - 2 Julia Donaldson 2 Julia Donaldson

Why is the array list not reflecting the first object values?

Because you create only one object and override second time when you set properties.

You need to create new object when you insert second object.

You are setting the value Julia Donaldson on the same Object. reference the variable Authors to a new object and the problem is solved.

authors Authors = new authors();
ArrayList<authors> tabAuthors = new ArrayList<authors>();

Authors.setAuthId(1);
Authors.setAuthName("Roald Dahl");
System.out.println(Authors.toString());
tabAuthors.add(Authors);

Authors = new Authors(); //Reference Authors to a new object here, or else 
//you're using the same object and you'll overwrite the value Roald Dahl with 
//Julia Donaldson 
Authors.setAuthId(2);
Authors.setAuthName("Julia Donaldson");
System.out.println(Authors.toString());
tabAuthors.add(Authors);

In addition to the answer above by @Maurice, you can also add a constructor in authors class to set values for every object.

public authors(int authId, String authName) {
        super();
        this.authId = authId;
        this.authName = authName;
}

In your main() , create two objects as below and add them to the list:

public static void main(String[] args) {

        Authors authors1 = new Authors(1, "Roald Dahl");
        Authors authors2 = new Authors(2, "Julia Donaldson");
        
        List<Authors> tabAuthors = new ArrayList<Authors>();

        System.out.println(authors1.toString());
        tabAuthors.add(authors1);

        System.out.println(authors2.toString());
        tabAuthors.add(authors2);

        for (int counter = 0; counter < tabAuthors.size(); counter++) {
        System.out.println(tabAuthors.get(counter).getAuthId() + " " + tabAuthors.get(counter).getAuthName());
      }
} 

Output:

authors{authId=1, authName='Roald Dahl'}
authors{authId=2, authName='Julia Donaldson'}
1 Roald Dahl
2 Julia Donaldson

Thanks for your prompt responses.
I added the below in the code as I need to add indeterminate number of objects - tabAuthors.add(new authors(authId,authName,authCountry,numBooks));

and declared authId,authName,authCountry,numBooks as variables. The object values are being fetched from a database table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM