1. 程式人生 > >30、Vector和Stack

30、Vector和Stack

Vector和Stack兩者用法差不多 Vector類中沒有addFirst()和addLast()

Vector vec=new Vector();
    Student s1=new Student(1,"胡歌");
    Student s2=new Student(2,"八戒");
    Student s3=new Student(3,"猴哥");
    vec.add(s1);
    vec.add(s2);
    vec.add(s3);
    System.out.println("大小為"+vec.size());
    //遍歷操作
    for (int
i=0;i<vec.size();i++){ // arr是ArrayList類,它是Object類的子類,它和Student類沒有關係,需要強轉 Student student= (Student) arr.get(i); System.out.println("第"+(i+1)+"個學生的姓名是"+student.getName()); }