數(shù)據(jù)加載中……
          java中的vector

          /** *//**
           * 我們設(shè)計的學生基本類
           
          */

          class Student
          {
              
          private String strName = "";//學生姓名
              private String strNumber = "";//學號
              private String strSex = "";//性別
              private String strBirthday = "";//出生年月
              private String strSpeciality = "";//專業(yè)
              private String strAddress = "";//地址

              
          public Student(String name, String number)
              
          {
                  strName 
          = name;
                  strNumber 
          = number;
              }


              
          public String getStudentName()
              
          {
                  
          return strName;
              }


              
          public String getStudentNumber()
              
          {
                  
          return strNumber;
              }


              
          public void setStudentSex(String sex)
              
          {
                  strSex 
          = sex;
              }


              
          public String getStudentSex()
              
          {
                  
          return strSex;
              }


              
          public String getStudentBirthday()
                  
          {
                  
          return strBirthday;
              }


              
          public void setStudentBirthday(String birthday)
              
          {
                  strBirthday 
          = birthday;
              }


              
          public String getStudentSpeciality()
              
          {
                  
          return strSpeciality;
              }


              
          public void setStudentSpeciality(String speciality)
              
          {
                  strSpeciality 
          = speciality;
              }


              
          public String getStudentAddress()
              
          {
                  
          return strAddress;
              }


              
          public void setStudentAddress(String address)
              
          {
                  strAddress 
          = address;
              }


              
          public String toString()
              
          {
                  String information 
          = "學生姓名=" + strName + ", 學號=" + strNumber;  
                  
          if!strSex.equals("") )
                      information 
          += ", 性別=" + strSex;
                  
          if!strBirthday.equals(""))
                      information 
          += ", 出生年月=" + strBirthday;
                  
          if!strSpeciality.equals("") )
                      information 
          += ", 專業(yè)=" + strSpeciality;
                  
          if!strAddress.equals("") )
                      information 
          += ", 籍貫=" + strAddress;
                  
          return information;
              }

          }













          /**
           * 通過這個程序,測試Vector的添加元素及插入元素
           
          */

           
          import java.util.Vector;

          public class VectorTest1
          {
              
          public static void main(String[] args)
              
          {
                  Vector vec 
          = new Vector();
                  
                  Student tom 
          = new Student("Tom","20020410");
                  Student jack 
          = new Student("Jack","20020411");
                  Student smith 
          = new Student("Smith","20020412");
                  
                  vec.add(tom);
                  vec.add(
          0, jack);//插入一個新的元素
                  vec.add(0, smith);//又插入一個新的元素
                  for(int i = 0; i < vec.size(); i++)
                  
          {
                      System.out.println(vec.get(i));
                  }

              }


          }







          /**
           * 通過這個程序,測試Vector的添加元素及插入元素
           
          */

           
          import java.util.Vector;

          public class VectorTest
          {
              
          public static void main(String[] args)
              
          {
                  Vector vec 
          = new Vector();
                  
                  Student tom 
          = new Student("Tom","20020410");
                  Student jack 
          = new Student("Jack","20020411");
                  Student smith 
          = new Student("Smith","20020412");
                  
                  vec.add(
          1, tom);
                  
          for(int i = 0; i < vec.size(); i++)
                  
          {
                      System.out.println(vec.get(i));
                  }

              }

          }





          /**
           * 通過這個程序,測試Vector的ratainAll方法
           
          */

           
          import java.util.Vector;

          public class VectorTest2
          {
              
          public static void main(String[] args)
              
          {
                  VectorTest2 test 
          = new VectorTest2();
                  Vector vec1 
          = new Vector();
                  Vector vec2 
          = new Vector();
                  
                  Student tom 
          = new Student("Tom","20020410");
                  Student jack 
          = new Student("Jack","20020411");
                  Student smith 
          = new Student("Smith","20020412");
                  Student rose 
          = new Student("Rose","20020413");
                  
                  vec1.add(tom);
                  vec1.add(jack);
                  vec1.add(smith);
                  vec1.add(rose);
                  System.out.println(
          "第一個Vector中的元素分別是:");
                  test.display(vec1);
                  
                  vec2.add(rose);
                  vec2.add(tom);
                  System.out.println(
          "\n第二個Vector中的元素分別是:");
                  test.display(vec2);
                  
                  System.out.println(
          "\n調(diào)用retainAll方法后,第一個Vector中的元素分別是:");
                  vec1.retainAll(vec2);
                  test.display(vec1);
                  
              }

              
              
          public void display(Vector vec)
              
          {
                  
          for(int i = 0; i < vec.size(); i++)
                  
          {
                      System.out.println(vec.get(i));
                  }

              }

          }


          posted on 2008-11-07 16:02 郭興華 閱讀(351) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 囊谦县| 虞城县| 兖州市| 龙泉市| 凤台县| 克拉玛依市| 商水县| 柳河县| 郁南县| 杭锦旗| 海城市| 泰宁县| 常熟市| 嘉定区| 耿马| 隆林| 巫溪县| 太保市| 万山特区| 丹凤县| 同仁县| 雷波县| 东乡县| 呼图壁县| 平遥县| 化州市| 祁门县| 英吉沙县| 清新县| 云梦县| 嘉义县| 郴州市| 深水埗区| 商河县| 临武县| 威信县| 平凉市| 南和县| 桐城市| 望奎县| 缙云县|