Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Initializing a particular class member in an Array List without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
I have a class
public class BeanTitle { private String btitle; public String getBtitle() { return btitle; } public void setBtitle(String btitle) { this.btitle = btitle; } }
In another class I am creating an ArrayList of BeanTitle Type and trying to initialize the “btitle”..
class xyz { void someMethod() { ArrayList<BeanTitle> bt=new ArrayList<BeanTitle>(); ... // How to initialize each "btitle" member here? } }
I am unsure of how to achieve this!
Answer
You can construct a new BeanTitle
object, set the field with the setter, and then add it to your list, like this:
BeanTitle b = new BeanTitle(); b.setBtitle("Jeff"); bt.add(b);
We are here to answer your question about Initializing a particular class member in an Array List - If you find the proper solution, please don't forgot to share this with your team members.