Tuesday, 14 January 2014

Java Programming Examples

What is the Output ???
public class Student {

int studentId;
String studentName;

public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
}

public class ArrayListEx {

public static void main(String[] args) {
List<Student> studentList = new ArrayList<Student>();
Student s1 = new Student();
s1.setStudentId(11);
s1.setStudentName("Raju");
studentList.add(s1);
Student s2 = new Student();
s2.setStudentId(22);
s2.setStudentName("Ravi");
studentList.add(s2);
for(int i=1; i<3; i++) {
Student s3 = new Student();
s3 = studentList.get(0);
s3.setStudentId(s3.getStudentId()+i);
s3.setStudentName(s3.getStudentName()+i);
studentList.add(s3);
}
Iterator<Student> itr = studentList.iterator();
while(itr.hasNext()) {
Student s = itr.next();
System.out.println(s.getStudentId());
System.out.println(s.getStudentName());
}
}

}