还有,字符串对象元素却在本例中能够搜索得到,
 Vector vt=new Vector();
 vt.add(new String("dog"));
 vt.add(new String("pig"));
 print(vt.indexOf(new String("dog")));//搜索到,返回0
 print(vt.indexOf("pig"));//搜索到,返回1
不知道为什么??

解决方案 »

  1.   

    我做了测试,下面的可行
    public static void main(String[] args){
        Vector v=new Vector();    for(int i=0;i<3;i++)
        {
           VectorTest a=new VectorTest(i);
          //int[] arr={0,i};
          //v.add(new VectorTest(i));
          v.add(a);
            //print(v.contains(new VectorTest(i) ));
          print(v.contains(a));
        }
       v.add(1,new VectorTest(123)); Enumeration e=v.elements();  //将生成的vector元素列表赋给e
       while(e.hasMoreElements())
       ((VectorTest)e.nextElement()).print();//必须类型造型,否则出错:
                               //究其原因:类型信息丢失在对象vector后
      print(v.size()); 
      

  2.   


       public static void main(String[] args){
    Vector v=new Vector();
    for(int i=0;i<3;i++)
    {
       VectorTest a=new VectorTest(i);
       v.add(a);
                                   
      print(v.contains(a));    //true,successful!  
    }
      
      print(v.indexOf(new VectorTest(1)));//-1,failure!
      print(v.indexOf(new VectorTest(2)));//-1,failure!
      print(v.contains(new VectorTest(2)));//false,failure!
      VectorTest flag=new VectorTest(123);
      v.add(flag);
      print(v.contains(flag));//true,successful!
      print(v.indexOf(flag)); //3,successful!
           
      Enumeration e=v.elements();  //将生成的vector元素列表赋给e
      while(e.hasMoreElements())
       ((VectorTest)e.nextElement()).print();//必须类型造型,否则出错:
                   //究其原因:类型信息丢失在对象vector后
         print(v.size()); 
     
      
       }
    真搞不懂了,JAVA就这么弱智吗??
      print(v.contains(new VectorTest(2)));//false,failure!
    没有给变量名就查找失败???up!!
    给出变量名就查找成功》》look down!!   VectorTest flag=new VectorTest(123);
      v.add(flag);
      print(v.contains(flag));//true,successful!
      

  3.   

    其实解决也很简单,只要在循环之前定一个数组就可以了public static void main(String[] args){
        Vector v=new Vector();    VectorTest[] a=new VectorTest[3];
        for(int i=0;i<3;i++)
        {
           a[i]=new VectorTest(i);
          
          v.add(a[i]);
           print(v.contains(a[i]));
        }
       v.add(1,new VectorTest(123));//下标为1的数组前插入元素
       Enumeration e=v.elements();  //将生成的vector元素列表赋给e
       while(e.hasMoreElements())
       ((VectorTest)e.nextElement()).print();//必须类型造型,否则出错:
                               //究其原因:类型信息丢失在对象vector后
      print(v.size());    //vector中元素个数   //v.clear();         //清空vector列表中所有元素
      print(v.isEmpty()); //判断vector列表是否为空
      print(v.contains(a[2]));//判断是否含有特定的对象元素;搜索不到((VectorTest)v.get(3)).print();  //取得vector列表中下标为3的数组的元素
     VectorTest vs=new VectorTest(123);
     //print(v.contains(vs));
     print(v.indexOf(a[1]));//判断是否含有特定的对象元素;搜索不到
      

  4.   

    其实我想这并不是java的弱智,只是因为你并没有实例化一个对象,所以当你用new class来创建一个对象的时候,它并不知道要找的是哪个对象,而String类型它本身并不需要实例化,这是我的理解