java util包概述

1.5 向量类vector

java.util.vector提供了向量(vector)类以实现类似动态数组的功能。在java语言中。正如在一开始就提到过,是没有指针概念 的,但如果能正确灵活地使用指针又确实可以大大提高程序的质量,比如在c、c++中所谓“动态数组”一般都由指针来实现。为了弥补这点缺陷,java提供 了丰富的类库来方便编程者使用,vector类便是其中之一。事实上,灵活使用数组也可完成向量类的功能,但向量类中提供的大量方法大大方便了用户的使 用。

创建了一个向量类的对象后,可以往其中随意地插入不同的类的对象,既不需顾及类型也不需预先选定向量的容量,并可方便地进行查找。对于预先不知或不愿预先定义数组大小,并需频繁进行查找、插入和删除工作的情况,可以考虑使用向量类。

向量类提供了三种构造方法:

public vector()

public vector(int initialcapacity,int capacityincrement)

public vector(int initialcapacity)

使用第一种方法,系统会自动对向量对象进行管理。若使用后两种方法,则系统将根据参数initialcapacity设定向量对象的容量(即向量对象可 存储数据的大小),当真正存放的数据个数超过容量时,系统会扩充向量对象的存储容量。参数capacityincrement给定了每次扩充的扩充值。当 capacityincrement为0时,则每次扩充一倍。利用这个功能可以优化存储。

在vector类中提供了各种方法方便用户使用:

■插入功能

(1)public final synchronized void addelement(object obj)

将obj插入向量的尾部。obj可以是任何类的对象。对同一个向量对象,可在其中插入不同类的对象。但插入的应是对象而不是数值,所以插入数值时要注意将数值转换成相应的对象。

例 要插入一个整数1时,不要直接调用v1.addelement(1),正确的方法为:

vector v1=new vector();

integer integer1=new 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。

了解了向量的最基本的方法后,我们来看一下例8.3vectorapp.java。

例1.3 vectorapp.java。

import java.util.vector;

java代码

import java.lang.*;//这一句不应该要,但原文如此

import java.util.enumeration;

public class vectorapp{

public static void main(string[] args){

vector v1=new vector();

integer integer1=new integer(1);

v1.addelement("one");

//加入的为字符串对象

v1.addelement(integer1);

v1.addelement(integer1);

//加入的为integer的对象

v1.addelement("two");

v1.addelement(new integer(2));

v1.addelement(integer1);

v1.addelement(integer1);

system.out.println("the vector v1 is:\n\t"+v1);

//将v1转换成字符串并打印

v1.insertelementat("three",2);

v1.insertelementat(new float(3.9),3);

system.out.println("the vector v1(used method insertelementat()) is:\n\t "+v1);

//往指定位置插入新的对象,指定位置后的对象依次往后顺延

v1.setelementat("four",2);

system.out.println("the vector v1(used method setelementat()) is:\n\t "+v1);

//将指定位置的对象设置为新的对象

v1.removeelement(integer1);

//从向量对象v1中删除对象integer1由于存在多个integer1所以从头开始

//找,删除找到的第一个integer1

"color: rgb(255, 0, 0);">enumeration enum=v1.elements();

system.out.print("the vector v1(used method removeelement())is:");

"font-weight: bold;">while(enum.hasmoreelements())

system.out.print"font-weight: bold;">(enum.nextelement()+" ");

system.out.println();

//使用枚举类(enumeration)的方法来获取向量对象的每个元素

system.out.println("the position of object 1(top-to-bottom):"

+ v1.indexof(integer1));

system.out.println("the position of object 1(tottom-to-top):"

+v1.lastindexof(integer1));

//按不同的方向查找对象integer1所处的位置

v1.setsize(4);

system.out.println("the new vector(resized the vector)is:"+v1);

//重新设置v1的大小,多余的元素被行弃

}

}

运行结果:

e:\java01>java 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>

import java.lang.*;//这一句不应该要,但原文如此import java.util.enumeration;public class vectorapp{public static void main(string[] args){vector v1=new vector();integer integer1=new integer(1);v1.addelement("one");//加入的为字符串对象v1.addelement(integer1);v1.addelement(integer1);//加入的为integer的对象v1.addelement("two");v1.addelement(new integer(2));v1.addelement(integer1);v1.addelement(integer1);system.out.println("the vector v1 is:\n\t"+v1);//将v1转换成字符串并打印v1.insertelementat("three",2);v1.insertelementat(new float(3.9),3);system.out.println("the vector v1(used method insertelementat()) is:\n\t "+v1);//往指定位置插入新的对象,指定位置后的对象依次往后顺延v1.setelementat("four",2);system.out.println("the vector v1(used method setelementat()) is:\n\t "+v1);//将指定位置的对象设置为新的对象v1.removeelement(integer1);//从向量对象v1中删除对象integer1由于存在多个integer1所以从头开始//找,删除找到的第一个integer1enumeration enum=v1.elements();system.out.print("the vector v1(used method removeelement())is:");while(enum.hasmoreelements())system.out.print(enum.nextelement()+" ");system.out.println();//使用枚举类(enumeration)的方法来获取向量对象的每个元素system.out.println("the position of object 1(top-to-bottom):"+ v1.indexof(integer1));system.out.println("the position of object 1(tottom-to-top):"+v1.lastindexof(integer1));//按不同的方向查找对象integer1所处的位置v1.setsize(4);system.out.println("the new vector(resized the vector)is:"+v1);//重新设置v1的大小,多余的元素被行弃}}运行结果:e:\java01>java vectorappthe 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 1the position of object 1(top-to-bottom):3the position of object 1(tottom-to-top):7the new vector(resized the vector)is:[one, four, 3.9, 1]e:\java01>

从例1.3运行的结果中可以清楚地了解上面各种方法的作用,另外还有几点需解释。

(1)类vector定义了方法

public final int size()

此方法用于获取向量元素的个数。它的返回值是向是中实际存在的元素个数,而非向量容量。可以调用方法capactly()来获取容量值。

方法:

public final synchronized void setsize(int newsize)

此方法用来定义向量大小。若向量对象现有成员个数已超过了newsize的值,则超过部分的多余元素会丢失。

(2)程序中定义了enumeration类的一个对象

enumeration是java.util中的一个接口类,在enumeration中封装了有关枚举数据集合的方法。

在enumeration中提供了方法hawmoreelement()来判断集合中是束还有其它元素和方法nextelement()来获取下一个元素。利用这两个方法可以依次获得集合中元素。

vector中提供方法:

public final synchronized enumeration elements()

此方法将向量对象对应到一个枚举类型。java.util包中的其它类中也大都有这类方法,以便于用户获取对应的枚举类型。

1.6 栈类stack

stack类是vector类的子类。它向用户提供了堆栈这种高级的数据结构。栈的基本特性就是先进后出。即先放入栈中的元素将后被推出。stack类中提供了相应方法完成栈的有关操作。

基本方法:

public object push(object hem)

将hem压入栈中,hem可以是任何类的对象。

public object pop()

弹出一个对象。

public object peek()

返回栈顶元素,但不弹出此元素。

public int search(object obj)

搜索对象obj,返回它所处的位置。

public boolean empty()

判别栈是否为空。

例1.4 stackapp.java使用了上面的各种方法。

例1.4 stackapp.java。

import java.lang.*;

java代码

import java.util.*;

public class stackapp{

public static void main(string args[]){

stack sta=new stack();

sta.push("apple");

sta.push("banana");

sta.push("cherry");

//压入的为字符串对象

sta.push(new integer(2));

//压入的为integer的对象,值为2

sta.push(new float(3.5));

//压入的为float的对象,值为3.5

system.out.println("the stack is,"+sta);

//对应栈sta

system.out.println("the top of stack is:"+sta.peek());

//对应栈顶元素,但不将此元素弹出

system.out.println("the position of object cherry is:"

+sta.search("cherry"));

//打印对象cherry所处的位置

system.out.print("pop the element of the stack:");

while(!sta.empty())

system.out.print(sta.pop()+" ");

system.out.println();

//将栈中的元素依次弹出并打印。与第一次打印的sta的结果比较,可看出栈

//先进后出的特点

}

}

运行结果(略)

import java.util.*;public class stackapp{public static void main(string args[]){stack sta=new stack();sta.push("apple");sta.push("banana");sta.push("cherry");//压入的为字符串对象sta.push(new integer(2));//压入的为integer的对象,值为2sta.push(new float(3.5));//压入的为float的对象,值为3.5system.out.println("the stack is,"+sta);//对应栈stasystem.out.println("the top of stack is:"+sta.peek());//对应栈顶元素,但不将此元素弹出system.out.println("the position of object cherry is:"+sta.search("cherry"));//打印对象cherry所处的位置system.out.print("pop the element of the stack:");while(!sta.empty())system.out.print(sta.pop()+" ");system.out.println();//将栈中的元素依次弹出并打印。与第一次打印的sta的结果比较,可看出栈//先进后出的特点}}运行结果(略)

1.7 哈希表类hashtable

哈希表是一种重要的存储方式,也是一种常见的检索方法。其基本思想是将关系码的值作为自变量,通过一定的函数关系计算出对应的函数值,把这个数值解释为 结点的存储地址,将结点存入计算得到存储地址所对应的存储单元。检索时采用检索关键码的方法。现在哈希表有一套完整的算法来进行插入、删除和解决冲突。在 java中哈希表用于存储对象,实现快速检索。

java.util.hashtable提供了种方法让用户使用哈希表,而不需要考虑其哈希表真正如何工作。

哈希表类中提供了三种构造方法,分别是:

public hashtable()

public hashtable(int initialcapacity)

public hashtable(int initialcapacity,float loadfactor)

参数initialcapacity是hashtable的初始容量,它的值应大于0。loadfactor又称装载因子,是一个0.0到0.1之间的 float型的浮点数。它是一个百分比,表明了哈希表何时需要扩充,例如,有一哈希表,容量为100,而装载因子为0.9,那么当哈希表90%的容量已被 使用时,此哈希表会自动扩充成一个更大的哈希表。如果用户不赋这些参数,系统会自动进行处理,而不需要用户操心。

hashtable提供了基本的插入、检索等方法。

■插入

public synchronized void put(object key,object value)

给对象value设定一关键字key,并将其加到hashtable中。若此关键字已经存在,则将此关键字对应的旧对象更新为新的对象value。这表明在哈希表中相同的关键字不可能对应不同的对象(从哈希表的基本思想来看,这也是显而易见的)。

■检索

public synchronized object get(object key)

根据给定关键字key获取相对应的对象。

public synchronized boolean containskey(object key)

判断哈希表中是否包含关键字key。

public synchronized boolean contains(object value)

判断value是否是哈希表中的一个元素。

■删除

public synchronized object remove(object key)

从哈希表中删除关键字key所对应的对象。

public synchronized void clear()

清除哈希表

另外,hashtalbe还提供方法获取相对应的枚举集合:

public synchronized enumeration keys()

返回关键字对应的枚举对象。

public synchronized enumeration elements()

返回元素对应的枚举对象。

例1.5 hashtable.java给出了使用hashtable的例子。

例1.5 hashtalbe.java。

//import java.lang.*;

java代码

import java.util.hashtable;

import java.util.enumeration;

public class hashapp{

public static void main(string args[]){

hashtable hash=new hashtable(2,(float)0.8);

//创建了一个哈希表的对象hash,初始容量为2,装载因子为0.8

hash.put("jiangsu","nanjing");

//将字符串对象“jiangsu”给定一关键字“nanjing”,并将它加入hash

hash.put("beijing","beijing");

hash.put("zhejiang","hangzhou");

system.out.println("the hashtable hash1 is: "+hash);

system.out.println("the size of this hash table is "+hash.size());

//打印hash的内容和大小

enumeration enum1=hash.elements();

system.out.print("the element of hash is: ");

while(enum1.hasmoreelements())

system.out.print(enum1.nextelement()+" ");

system.out.println();

//依次打印hash中的内容

if(hash.containskey("jiangsu"))

system.out.println("the capatial of jiangsu is "+hash.get("jiangsu"));

hash.remove("beijing");

//删除关键字beijing对应对象

system.out.println("the hashtable hash2 is: "+hash);

system.out.println("the size of this hash table is "+hash.size());

}

}

运行结果:

the hashtable hash1 is: {beijing=beijing, zhejiang=hangzhou, jiangsu=nanjing}

the size of this hash table is 3

the element of hash is: beijing hangzhou nanjing

the capatial of jiangsu is nanjing

the hashtable hash2 is: {zhejiang=hangzhou, jiangsu=nanjing}

the size of this hash table is 2

import java.util.hashtable;import java.util.enumeration;public class hashapp{public static void main(string args[]){hashtable hash=new hashtable(2,(float)0.8);//创建了一个哈希表的对象hash,初始容量为2,装载因子为0.8hash.put("jiangsu","nanjing");//将字符串对象“jiangsu”给定一关键字“nanjing”,并将它加入hashhash.put("beijing","beijing");hash.put("zhejiang","hangzhou");system.out.println("the hashtable hash1 is: "+hash);system.out.println("the size of this hash table is "+hash.size());//打印hash的内容和大小enumeration enum1=hash.elements();system.out.print("the element of hash is: ");while(enum1.hasmoreelements())system.out.print(enum1.nextelement()+" ");system.out.println();//依次打印hash中的内容if(hash.containskey("jiangsu"))system.out.println("the capatial of jiangsu is "+hash.get("jiangsu"));hash.remove("beijing");//删除关键字beijing对应对象system.out.println("the hashtable hash2 is: "+hash);system.out.println("the size of this hash table is "+hash.size());}}运行结果:the hashtable hash1 is: {beijing=beijing, zhejiang=hangzhou, jiangsu=nanjing}the size of this hash table is 3the element of hash is: beijing hangzhou nanjingthe capatial of jiangsu is nanjingthe hashtable hash2 is: {zhejiang=hangzhou, jiangsu=nanjing}the size of this hash table is 2

hashtable是dictionary(字典)类的子类。在字典类中就把关键字对应到数据值。字典类是一个抽象类。在java.util中还有一个类properties,它是hashtable的子类。用它可以进行与对象属性相关的操作。


======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值