Collections

How to add class object to arraylist in java

package com.candidjava;

import java.util.ArrayList;
import java.util.Iterator;

class Student {
  int rollno;
  String name;
  int age;

  Student(int rollno, String name, int age) {
    this.rollno = rollno;
    this.name = name;
    this.age = age;
  }

  public int getRollno() {
    return rollno;
  }

  public void setRollno(int rollno) {
    this.rollno = rollno;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

}

public class ArraylistaddObject {
  public static void main(String[] args) {

    Student s1 = new Student(101, "kavi", 15);
    Student s2 = new Student(102, "ram", 15);
    Student s3 = new Student(103, "sam", 15);
    // Adding objects into array list
    ArrayList<Student> al = new ArrayList<Student>();
    al.add(s1);
    al.add(s2);
    al.add(s3);
    Iterator itr = al.iterator();
    while (itr.hasNext()) {
      Student st = (Student) itr.next();
      System.out.println(st.getRollno() + " " + st.getName() + " " + st.getAge());
    }

  }

}