wordpress网站整站搬迁,免费推广网站58,固原网站建设,那些行业需要做网站摘自百度百科#xff1a; 1. Vector 类在 java 中可以实现自动增长的对象数组; 创建了一个向量类的对象后#xff0c;可以往其中随意地插入不同的类的对象#xff0c;既不需顾及类型也不需预先选定向量的容量#xff0c;并可方便地进行查找。对于预先不知或不愿预先定义数组…摘自百度百科 1. Vector 类在 java 中可以实现自动增长的对象数组; 创建了一个向量类的对象后可以往其中随意地插入不同的类的对象既不需顾及类型也不需预先选定向量的容量并可方便地进行查找。对于预先不知或不愿预先定义数组大小并需频繁进行查找、插入和删除工作的情况可以考虑使用向量类。向量类提供了三种构造方法public Vector()public Vector(int initialcapacity,int capacityIncrement)public Vector(int initialcapacity)使用第一种方法系统会自动对向量对象进行管理。若使用后两种方法则系统将根据参数initialcapacity设定向量对象的容量即向量对象可存储数据的大小当真正存放的数据个数超过容量时系统会扩充向量对象的存储容量。参数capacityIncrement给定了每次扩充的扩充值。当capacityIncrement为0时则每次扩充一倍。利用这个功能可以优化存储。2. Java中数组对象一旦创建后其元素的个数 不能被修改。而Java.util包中的Vector类向量提供类似于数组的能力且能够动态地调整自身的大小。Vector类似于一个数组但与数组相比在使用上有两个优点 ① 使用的时候无须声明上限随着元素的增加Vector的长度会自动增加 ② Vector类提供额外的方法来增加、删除元素比数组操作高效。[1] 插入功能 (1)public final synchronized void addElement(Object obj) 将obj插入向量的尾部。obj可以是任何类的对象。对同一个向量对象可在其中插入不同类的对象。但插入的应是对象而不是数值所以插入数值时要注意将数值转换成相应的对象。 例 要插入一个整数1时不要直接调用v1.addElement(1正确的方法为 1 2 3 Vectorv1new Vector(); Integerinteger1new Integer(1); v1.addElement(integer1); (2)public final synchronized void setElementAt(object obj,int index) 将index处的对象设成obj原来的对象将被覆盖。 (3)public final synchronized void insertElementAt(Object obj,int index) 在index指定的位置插入obj原来对象以及此后的对象依次往后顺延。 删除功能 (1)public final synchronized void removeElement(Object obj) 从向量中删除obj。若有多个存在则从向量头开始试删除找到的第一个与obj相同的向量成员。 (2)public final synchronized void removeAllElement() 删除向量中所有的对象。 (3)public final synchronized void removeElementlAt(int index) 删除index所指的地方的对象。 查询搜索功能 (1)public final int indexOf(Object obj) 从向量头开始搜索obj返回所遇到的第一个obj对应的下标若不存在此obj返回-1。 (2)public final synchronized int indexOf(Object obj,int index) 从index所表示的下标处开始搜索obj。 (3)public final int lastIndexOf(Object obj) 从向量尾部开始逆向搜索obj。 (4)public final synchronized int lastIndexOf(Object obj,int index) 从index所表示的下标处由尾至头逆向搜索obj。 (5)public final synchronized Object firstElement() 获取向量对象中的首个obj。 (6)public final synchronized Object lastElement() 获取向量对象中的最后一个obj。 实例 了解了向量的最基本的方法后我们来看一下例子VectorApp.java。 例 VectorApp.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 importjava.util.Vector; importjava.lang.*; //这一句不应该要但原文如此 importjava.util.Enumeration; public class VectorApp { public static void main(String[]args) { VectorInteger v1new VectorInteger();//jdk1.5以后增加了对的支持 Integer integer1new Integer(1); /** *因为VectorInteger已经指定为Integer类型 *当v1.addElement(one)时会报错类型不符 *ThemethodaddElement(Integer)inthetypeVectorIntegerisnotapplicablefor *thearguments(String) */ //加入的为字符串对象 v1.addElement(one); v1.addElement(two); //加入的为Integer的对象 v1.addElement(integer1); v1.addElement(newInteger(2)); System.out.println(Thevectorv1is:\n\tv1); //此处的输出结果为Thevectorv1is:[1,2] /** *ThemethodinsertElementAt(Integer,int)inthetypeVectorIntegerisnotapplicablefor *thearguments(String,int) *insertElementAt()是指Insertsthespecifiedobjectasacomponentinthisvectorat *thespecifiedindex. */ //将v1转换成字符串并打印 v1.insertElementAt(three,2); v1.insertElementAt(newFloat(3.9),3); System.out.println(Thevectorv1(usedmethodinsertElementAt())is:\n\tv1); //以上运行请改VectorInteger为VectorFloat /** *setElementAt(Eobj,intindex)的用法 *Setsthecomponentatthespecifiedindexofthisvectortobethespecifiedobject. *Thepreviouscomponentatthatpositionisdiscarded. *Theindexmustbeavaluegreaterthanorequalto0andlessthanthecurrentsizeofthevector. */ //将指定位置的对象设置为新的对象 v1.setElementAt(2,1); System.out.println(Thevectorv1(usedmethodsetElementAt())is:\n\tv1); //从向量对象v1中删除对象integer1由于存在多个integer1所以从头开始找删除找到的第一个integer1 v1.removeElement(integer1); //使用枚举类Enumeration)的方法来获取向量对象的每个元素 Enumerationenumv1.elements(); System.out.print(Thevectorv1(usedmethodremoveElement())is:); while(enum.hasMoreElements()) System.out.print(enum.nextElement()); System.out.println(); System.out.println(Thepositionofobject1(top-to-bottom): v1.indexOf(integer1)); System.out.println(Thepositionofobject1(tottom-to-top): v1.lastIndexOf(integer1)); //按不同的方向查找对象integer1所处的位置 v1.setSize(4); System.out.println(Thenewvector(resizedthevector)is:v1); //重新设置v1的大小多余的元素被行弃 } } 运行结果 E:\java01java VectorApp The vector v1 is: [one,1,1,two,2,1,1] The vector v1(used method insertElementAt())is: [one,1,three,3.9,1,two,2,1,1] The vector v1(used method setElementAt()) is: [one,1,four,3.9,1,two,2,1,1] The vector v1(used method removeElement())is: one four 3.9 1 two 2 1 1 The position of object 1(top-to-bottom):3 The position of object 1(tottom-to-top):7 The new vector(resized the vector)is: [one,four,3.9,1] E:\java01 从例1中运行的结果中可以清楚地了解上面各种方法的作用另外还有几点需解释。 (1)类Vector定义了方法 public final int size() 此方法用于获取向量元素的个数。它的返回值是向量中实际存在的元素个数而非向量容量。可以调用方法capactly来获取容量值。 方法 public final synchronized void setsize(int newsize) 此方法用来定义向量大小。若向量对象现有成员个数已超过了newsize的值则超过部分的多余元素会丢失。 (2)程序中定义了Enumeration类的一个对象 Enumeration是java.util中的一个接口类在Enumeration中封装了有关枚举数据集合的方法。 在Enumeration中提供了方法hasMoreElement来判断集合中是否还有其它元素和方法nextElement来获取下一个元素。利用这两个方法可以依次获得集合中元素。 Vector中提供方法 public final synchronized Enumeration elements() 此方法将向量对象对应到一个枚举类型。java.util包中的其它类中也大都有这类方法以便于用户获取对应的枚举类型。Java中数组对象一旦创建后其元素的个数 不能被修改。而Java.util包中的Vector类向量提供类似于数组的能力且能够动态地调整自身的大小。Vector类似于一个数组但与数组相比在使用上有两个优点 ① 使用的时候无须声明上限随着元素的增加Vector的长度会自动增加 ② Vector类提供额外的方法来增加、删除元素比数组操作高效。[1] 插入功能 (1)public final synchronized void addElement(Object obj) 将obj插入向量的尾部。obj可以是任何类的对象。对同一个向量对象可在其中插入不同类的对象。但插入的应是对象而不是数值所以插入数值时要注意将数值转换成相应的对象。 例 要插入一个整数1时不要直接调用v1.addElement(1正确的方法为 1 2 3 Vectorv1new Vector(); Integerinteger1new Integer(1); v1.addElement(integer1); (2)public final synchronized void setElementAt(object obj,int index) 将index处的对象设成obj原来的对象将被覆盖。 (3)public final synchronized void insertElementAt(Object obj,int index) 在index指定的位置插入obj原来对象以及此后的对象依次往后顺延。 删除功能 (1)public final synchronized void removeElement(Object obj) 从向量中删除obj。若有多个存在则从向量头开始试删除找到的第一个与obj相同的向量成员。 (2)public final synchronized void removeAllElement() 删除向量中所有的对象。 (3)public final synchronized void removeElementlAt(int index) 删除index所指的地方的对象。 查询搜索功能 (1)public final int indexOf(Object obj) 从向量头开始搜索obj返回所遇到的第一个obj对应的下标若不存在此obj返回-1。 (2)public final synchronized int indexOf(Object obj,int index) 从index所表示的下标处开始搜索obj。 (3)public final int lastIndexOf(Object obj) 从向量尾部开始逆向搜索obj。 (4)public final synchronized int lastIndexOf(Object obj,int index) 从index所表示的下标处由尾至头逆向搜索obj。 (5)public final synchronized Object firstElement() 获取向量对象中的首个obj。 (6)public final synchronized Object lastElement() 获取向量对象中的最后一个obj。 实例 了解了向量的最基本的方法后我们来看一下例子VectorApp.java。 例 VectorApp.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 importjava.util.Vector; importjava.lang.*; //这一句不应该要但原文如此 importjava.util.Enumeration; public class VectorApp { public static void main(String[]args) { VectorInteger v1new VectorInteger();//jdk1.5以后增加了对的支持 Integer integer1new Integer(1); /** *因为VectorInteger已经指定为Integer类型 *当v1.addElement(one)时会报错类型不符 *ThemethodaddElement(Integer)inthetypeVectorIntegerisnotapplicablefor *thearguments(String) */ //加入的为字符串对象 v1.addElement(one); v1.addElement(two); //加入的为Integer的对象 v1.addElement(integer1); v1.addElement(newInteger(2)); System.out.println(Thevectorv1is:\n\tv1); //此处的输出结果为Thevectorv1is:[1,2] /** *ThemethodinsertElementAt(Integer,int)inthetypeVectorIntegerisnotapplicablefor *thearguments(String,int) *insertElementAt()是指Insertsthespecifiedobjectasacomponentinthisvectorat *thespecifiedindex. */ //将v1转换成字符串并打印 v1.insertElementAt(three,2); v1.insertElementAt(newFloat(3.9),3); System.out.println(Thevectorv1(usedmethodinsertElementAt())is:\n\tv1); //以上运行请改VectorInteger为VectorFloat /** *setElementAt(Eobj,intindex)的用法 *Setsthecomponentatthespecifiedindexofthisvectortobethespecifiedobject. *Thepreviouscomponentatthatpositionisdiscarded. *Theindexmustbeavaluegreaterthanorequalto0andlessthanthecurrentsizeofthevector. */ //将指定位置的对象设置为新的对象 v1.setElementAt(2,1); System.out.println(Thevectorv1(usedmethodsetElementAt())is:\n\tv1); //从向量对象v1中删除对象integer1由于存在多个integer1所以从头开始找删除找到的第一个integer1 v1.removeElement(integer1); //使用枚举类Enumeration)的方法来获取向量对象的每个元素 Enumerationenumv1.elements(); System.out.print(Thevectorv1(usedmethodremoveElement())is:); while(enum.hasMoreElements()) System.out.print(enum.nextElement()); System.out.println(); System.out.println(Thepositionofobject1(top-to-bottom): v1.indexOf(integer1)); System.out.println(Thepositionofobject1(tottom-to-top): v1.lastIndexOf(integer1)); //按不同的方向查找对象integer1所处的位置 v1.setSize(4); System.out.println(Thenewvector(resizedthevector)is:v1); //重新设置v1的大小多余的元素被行弃 } } 运行结果 E:\java01java VectorApp The vector v1 is: [one,1,1,two,2,1,1] The vector v1(used method insertElementAt())is: [one,1,three,3.9,1,two,2,1,1] The vector v1(used method setElementAt()) is: [one,1,four,3.9,1,two,2,1,1] The vector v1(used method removeElement())is: one four 3.9 1 two 2 1 1 The position of object 1(top-to-bottom):3 The position of object 1(tottom-to-top):7 The new vector(resized the vector)is: [one,four,3.9,1] E:\java01 从例1中运行的结果中可以清楚地了解上面各种方法的作用另外还有几点需解释。 (1)类Vector定义了方法 public final int size() 此方法用于获取向量元素的个数。它的返回值是向量中实际存在的元素个数而非向量容量。可以调用方法capactly来获取容量值。 方法 public final synchronized void setsize(int newsize) 此方法用来定义向量大小。若向量对象现有成员个数已超过了newsize的值则超过部分的多余元素会丢失。 (2)程序中定义了Enumeration类的一个对象 Enumeration是java.util中的一个接口类在Enumeration中封装了有关枚举数据集合的方法。 在Enumeration中提供了方法hasMoreElement来判断集合中是否还有其它元素和方法nextElement来获取下一个元素。利用这两个方法可以依次获得集合中元素。 Vector中提供方法 public final synchronized Enumeration elements() 此方法将向量对象对应到一个枚举类型。java.util包中的其它类中也大都有这类方法以便于用户获取对应的枚举类型。 3. vector 是同一种类型的对象的集合,每个对象都有一个对应的整数索引值[2] 。 和 string 对象一样,标准库将负责管理与存储元素相关的内存。我们把 vector称为容器,是因为它可以包含其他对象能够存放任意类型的动态数组增加和压缩数据。一个容器中的所有对象都必须是同一种类型的[2]。 vector 是一个类模板(class template)。使用模板可以编写一个类定义或函数定义,而用于多个不同的数据类型。因此,我们可以定义保存 string 对象的 vector,或保存 int 值的 vector,又或是保存自定义的类类型对象(如Sales_items 对象)的 vector。vector 不是一种数据类型,而只是一个类模板,可用来定义任意多种数据类型。vector 类型的每一种都指定了其保存元素的类型