Thursday, 21 May 2015

How to storing Java Object In Vector

import java.util.Vector;

public class VectorOfObject {

    public static void main(String[] args) {

        Vector<User> vector = new Vector<User>();
        vector.add(new User("shivam",100));
        vector.add(new User("raja",200));
        vector.add(new User("mukesh",300));
        vector.add(new User("piyush",500));
       
        // print vector
        for (User user : vector) {
            System.out.println("user name="+user.getName()+" User id="+user.getUserId());
        }
   
    }
   
   
}

class User{

    private String name;
    private int userId;
   
    public User(String name, int userId) {
        super();
        this.name = name;
        this.userId = userId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getUserId() {
        return userId;
    }
    public void setUserId(int userId) {
        this.userId = userId;
    }
   
}


Output:

user name=shivam User id=100

user name=raja User id=200

user name=mukesh User id=300

user name=piyush User id=500

No comments:

Post a Comment