P.Ambh P.Ambh - 1 month ago 11
Java Question

How to populate a collection in java?

Suppose we are creating a class in java where name and code are two private members and then are passing them in the list.

class A
{
private int code;
private string name;
public getCode()
{ return code;}
public setCode(int code){this.code=code;}
public getName()
{ return name;}
public setName(String name){this.name=name;}





class B {
List<A> l1=new List<A>


Now how to populate list A?

Answer
A obj = new A();
obj.setCode(123);
obj.setName("test");

List<A> list = new ArrayList<>();
list.add(obj);
Comments