import java.util.Enumeration;
import java.util.Vector;
public class Demo01 {
public static void main(String[] args) {
Vector:数组结构实现,查询快。增删慢 ;运行效率慢,线程安全
//创建集合
Vector vector = new Vector<>();
//1.添加元素
Student s1 = new Student("张三", 16);
Student s2 = new Student("李四", 16);
Student s3 = new Student("王五", 16);
vector.add(s1);
vector.add(s2);
vector.add(s3);
System.out.println("元素个数:"+vector.size());//元素个数:3
System.out.println(vector);//[Student{name='张三', age=16}, Student{name='李四', age=16}, Student{name='王五', age=16}]
//2.删除元素
vector.remove(s1);
System.out.println("元素个数:"+vector.size());//元素个数:2
System.out.println(vector);//[Student{name='李四', age=16}, Student{name='王五', age=16}]
//3.遍历
//使用枚举器
Enumeration elements = vector.elements();
while (elements.hasMoreElements()){
System.out.println(elements.nextElement());
}
//4.判断
System.out.println(vector.contains(s2));//true
System.out.println(vector.isEmpty());//false
//5.其他方法
System.out.println(vector.firstElement());//Student{name='李四', age=16}
System.out.println(vector.lastElement());//Student{name='王五', age=16}
System.out.println(vector.elementAt(0));//Student{name='李四', age=16}
}
}
public class Student {标签:name,Vector,16,age,vector,Student,使用,public From: https://www.cnblogs.com/123456dh/p/17133145.html
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object obj) {
//1.判断是否为空
if(obj==null){
return false;
}
//2.判断是否为同一个对象
if(obj==this){
return true;
}
//判断是否可转为Student类型
if(obj instanceof Student){
Student s=(Student) obj;//强转
if(this.name.equals(s.getName())&&this.age==s.getAge()){
return true;
}
}
return false;
}
}