import java.util.*;public class VectorTest
{
private int i=0; VectorTest(){} VectorTest(int i){
this.i=i;
} public void print(){
System.out.println(i);
} public static void print(int i){
System.out.println(i);
} public static void print(String str){
System.out.println(str);
} public static void print(boolean b){
System.out.println(b);
} public static void main(String[] args){
Vector v=new Vector(); for(int i=0;i<10;i++)
v.add(new VectorTest(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(new VectorTest(3)));//判断是否含有特定的对象元素;搜索不到
((VectorTest)v.get(3)).print(); //取得vector列表中下标为3的数组的元素
VectorTest vs=new VectorTest(3);
print(v.indexOf(vs));//判断是否含有特定的对象元素;搜索不到 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
}
}Vector对象有很多方法,其中有方法
public boolean contains(Object obj)
public int indexOf(Object obj)
public synchronized Object[] toArray()
但是我在本例使用的过程中却发现不能如愿以尝地使用以上方法,不知道
我程序中问题出在哪里?
问题:
Vector v=new Vector();
for(int i=0;i<10;i++)
v.add(new VectorTest(i));
v.contains(new VectorTest(i))//失败
v.indexOf(new VectorTest(i))//失败
想想将对象add入Vector列表以后,对象的类型信息被丢失掉了,也就是说
Vector对象v中根本无元素的类型信息,这也不难理解为什么搜索不到
但是这样的话不是说明这些方法根本不可用吗?
谢谢各位大虾帮忙!!!
{
private int i=0; VectorTest(){} VectorTest(int i){
this.i=i;
} public void print(){
System.out.println(i);
} public static void print(int i){
System.out.println(i);
} public static void print(String str){
System.out.println(str);
} public static void print(boolean b){
System.out.println(b);
} public static void main(String[] args){
Vector v=new Vector(); for(int i=0;i<10;i++)
v.add(new VectorTest(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(new VectorTest(3)));//判断是否含有特定的对象元素;搜索不到
((VectorTest)v.get(3)).print(); //取得vector列表中下标为3的数组的元素
VectorTest vs=new VectorTest(3);
print(v.indexOf(vs));//判断是否含有特定的对象元素;搜索不到 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
}
}Vector对象有很多方法,其中有方法
public boolean contains(Object obj)
public int indexOf(Object obj)
public synchronized Object[] toArray()
但是我在本例使用的过程中却发现不能如愿以尝地使用以上方法,不知道
我程序中问题出在哪里?
问题:
Vector v=new Vector();
for(int i=0;i<10;i++)
v.add(new VectorTest(i));
v.contains(new VectorTest(i))//失败
v.indexOf(new VectorTest(i))//失败
想想将对象add入Vector列表以后,对象的类型信息被丢失掉了,也就是说
Vector对象v中根本无元素的类型信息,这也不难理解为什么搜索不到
但是这样的话不是说明这些方法根本不可用吗?
谢谢各位大虾帮忙!!!
v.contains(new VectorTest(i))
当然会失败, contains做引用比较, 你重新new的对象,vector中当然没有了
class A:有
(new A()).equals(new A()) = false
而java自带的class则不同
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!
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!
public boolean contains(Object elem) {
return indexOf(elem, 0) >= 0;
}
public synchronized int indexOf(Object elem, int index) {
if (elem == null) {
for (int i = index ; i < elementCount ; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = index ; i < elementCount ; i++)
if (elem.equals(elementData[i]))
return i;
}
return -1;
}因为VectorTest的超类是Object,即
VectorTest vt_1 = new VectorTest() ;
VectorTest vt_2 = new VectorTest() ;那么vt_1 != vt_2 ;
vt_1.equals(vt_2) == false ;
因此
v.add(vt_1) ;
v.contains(vt_2) == false ;