I have to create a collection of Name,Address,Age.How do I create this as a collection object in java.
My snippet is as follows:
public class DataCollection implements java.io.Serializable{
private String Name;
private String Address;
private int Age;
}
And I have the getter and setter methods...
In the main method, how do I create this as a collection??
list.add(new DataCollection(??????) );
Can someone please help me on this?
public class DataCollection implements java.io.Serializable{
private String Name;
private String Address;
private int Age;
public DataCollection(String name, String address, int age){
this.Name=name;
this.Address=address;
this.Age=age;
}
}
After that create DataCollection objects:
DataCollection d1 = new DataCollection("nik", "10/5 cross", 20);//creation of List collection
Now put the object inside a collection:
List<DataCollection> list = new LinkedList<DataCollection>();
list.add(d1);
You can iterate like below:
List<DataCollection> list=new LinkedList<DataCollection>();
for(DataCollection d : list){
System.out.println(d);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With