/* The Vector class implements a growable array of objects.
* Like an array, it contains components that can be accessed using an integer index.
* However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
*/
package collection.list;
import java.util.Collection;
import java.util.Collections;
import java.util.Vector;
public class VectorDemo {
public VectorDemo() {
super();
}
public static void main(String []Args){
/*
* vector containing Integers
*/
simpleVector();
/*
* vector containing objects of class Student
*/
Vector<Student> students=vectorOfStudents();
}
public static void simpleVector(){
Vector<Integer> vector= new Vector<Integer>();
vector.add(1);
vector.add(21);
vector.add(113);
vector.add(14);
vector.add(51);
vector.add(67);
vector.add(89);
vector.add(45);
System.out.println("Vector elements");
for(Integer v:vector){
System.out.println(v);
}
System.out.println("\nAfter Sorting elements");
Collections.sort(vector);
for(Integer v:vector){
System.out.println(v);
}
}
private static Vector<Student> vectorOfStudents() {
Vector<Student> studentVector= new Vector<Student>();
studentVector.add(new Student(12,"sachin","AB+"));
studentVector.add(new Student(25,"Rahul","B+"));
studentVector.add(new Student(82,"Gaurav","A-"));
studentVector.add(new Student(41,"Dravid","AB-"));
studentVector.add(new Student(20,"Dhoni","B-"));
studentVector.add(new Student(89,"Mohan","AB-"));
for(Student std:studentVector ){
System.out.println(std);
}
return studentVector;
}
}
* Like an array, it contains components that can be accessed using an integer index.
* However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
*/
package collection.list;
import java.util.Collection;
import java.util.Collections;
import java.util.Vector;
public class VectorDemo {
public VectorDemo() {
super();
}
public static void main(String []Args){
/*
* vector containing Integers
*/
simpleVector();
/*
* vector containing objects of class Student
*/
Vector<Student> students=vectorOfStudents();
}
public static void simpleVector(){
Vector<Integer> vector= new Vector<Integer>();
vector.add(1);
vector.add(21);
vector.add(113);
vector.add(14);
vector.add(51);
vector.add(67);
vector.add(89);
vector.add(45);
System.out.println("Vector elements");
for(Integer v:vector){
System.out.println(v);
}
System.out.println("\nAfter Sorting elements");
Collections.sort(vector);
for(Integer v:vector){
System.out.println(v);
}
}
private static Vector<Student> vectorOfStudents() {
Vector<Student> studentVector= new Vector<Student>();
studentVector.add(new Student(12,"sachin","AB+"));
studentVector.add(new Student(25,"Rahul","B+"));
studentVector.add(new Student(82,"Gaurav","A-"));
studentVector.add(new Student(41,"Dravid","AB-"));
studentVector.add(new Student(20,"Dhoni","B-"));
studentVector.add(new Student(89,"Mohan","AB-"));
for(Student std:studentVector ){
System.out.println(std);
}
return studentVector;
}
}
Comments
Post a Comment