JAVA - How To Use List In Java NetBeans With Class
__________________________________________________________________________
In This Java Code We Will See How To Use List With Class In Java Programming Language.
Project Source Code:
In This Java Code We Will See How To Use List With Class In Java Programming Language.
Project Source Code:
package javadb_001;
import java.util.ArrayList;
import java.util.List;
public class Users {
private int id;
private String fname;
private String lname;
private int age;
private int pos = 0;
public Users(){}
public Users(int _id,String _fname,String _lname,int _age){
this.id = _id;
this.fname = _fname;
this.lname = _lname;
this.age = _age;
}
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public String getFname(){
return this.fname;
}
public void setFname(String fname){
this.fname = fname;
}
public String getLname(){
return this.lname;
}
public void setLname(String lname){
this.lname = lname;
}
public int getAge(){
return this.age;
}
public void setAge(int age){
this.age = age;
}
public String showInfo(){
return this.id+" - "+this.fname+" - "+this.lname+" - "+this.age;
}
public static void main(String[] args){
List<Users> user = new ArrayList<Users>();
Users u1 = new Users(1,"A1","B1",10);
Users u2 = new Users(2,"A2","B2",20);
Users u3 = new Users(3,"A3","B3",30);
Users u4 = new Users(4,"A4","B4",40);
user.add(u1);
user.add(u2);
user.add(u3);
user.add(u4);
for(int i = 0;i<user.size();i++)
{
System.out.println(user.get(i).showInfo());
}
}
}
////////////////////////////////// OUTPUT: 1 - A1 - B1 - 10
2 - A2 - B2 - 20
3 - A3 - B3 - 30
4 - A4 - B4 - 40
////////////////////////////////// OUTPUT: 1 - A1 - B1 - 10
2 - A2 - B2 - 20
3 - A3 - B3 - 30
4 - A4 - B4 - 40