dzungchaos

Java: VD về Vector

Jul 2nd, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package Loptienoch;
  2.  
  3. import java.util.*;
  4.  
  5. public class VectorExample {
  6.     public static void main(String args[]) {
  7.         Vector ds = new Vector();
  8.         //Thêm các phần tử vào ds
  9.         ds.addElement("Nguyen Van A");
  10.         ds.addElement("Tran Thi B");
  11.         ds.addElement("Le Thi C");
  12.         ds.addElement("Ho D");
  13.  
  14.         String ht= "Tran Thi B";
  15.         if (ds.contains(ht))
  16.             System.out.println("Có SV " + ht + " tại vị trí " + ds.indexOf(ht));
  17.         else
  18.             System.out.println("Không có SV " + ht);
  19.  
  20.         Enumeration e = ds.elements();
  21.         while (e.hasMoreElements())
  22.             System.out.println(e.nextElement());
  23.  
  24.         //Xóa phần tử
  25.         ds.removeElement("Nguyen Van A");
  26.         System.out.println(ds);
  27.  
  28.         System.out.println();
  29.  
  30.         Vector ds1 = new Vector();
  31.         //Thêm các phần tử vào ds
  32.         Integer i1 = 1;
  33.         Integer i2 = 2;
  34.         Integer i3 = 3;
  35.         ds1.addElement(i1);
  36.         ds1.addElement(i2);
  37.         ds1.addElement(i3);
  38.         Integer i_1 = 1;
  39.  
  40.         if (ds1.contains(i_1)){
  41.             System.out.println("Co 1");
  42.         }
  43.         else System.out.println("Khong co 1");
  44.  
  45.         System.out.println();
  46.  
  47.         Vector ds2 = new Vector();
  48.         ABC abc1 = new ABC(1, 1, "lol");
  49.         ABC abc2 = new ABC(1, 2, "zol");
  50.         ABC abc3 = new ABC(2, 1, "xol");
  51.         ABC abc_1 = new ABC(1, 1, "lol");
  52.         ds2.addElement(abc1);
  53.         ds2.addElement(abc2);
  54.         ds2.addElement(abc3);
  55.         if (ds2.contains(abc_1))
  56.             System.out.println("Co abc_1");
  57.         else
  58.             System.out.println("Ko co abc_1");
  59.  
  60.         if (ds2.contains(abc1))
  61.             System.out.println("Co abc1");
  62.         else
  63.             System.out.println("Ko co abc1");
  64.  
  65.         System.out.println(abc1.equals(abc_1));
  66.  
  67.         System.out.println();
  68.  
  69.         Enumeration e2 = ds2.elements();
  70.         while (e2.hasMoreElements()){
  71.             ABC myabc = (ABC)e2.nextElement();
  72.             System.out.println(myabc.x);
  73.             System.out.println(myabc.y);
  74.             System.out.println(myabc.z);
  75.         }
  76.  
  77.     }
  78. }
  79.  
  80. class ABC{
  81.     int x;
  82.     float y;
  83.     String z;
  84.  
  85.     public ABC(int x, long y, String z){
  86.         this.x = x;
  87.         this.y = y;
  88.         this.z = z;
  89.     }
  90.    
  91.    
  92.     public boolean equals(Object object){
  93.         ABC myabc = (ABC)object;
  94.         if ((this.x == myabc.x) && (this.y == myabc.y) && (this.z == myabc.z))
  95.             return true;
  96.         else
  97.             return false;
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment