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
- <span style="color: rgb(255, 0, 0);"> </span><span style="font-weight: bold;"><span style="color: rgb(255, 0, 0);">Enumeration enum=v1.elements();</span></span><span style="color: rgb(255, 0, 0);">
- System.out.print("The vector v1(used method removeElement())is:");
- </span><span style="font-weight: bold;"><span style="color: rgb(255, 0, 0);">while(enum.hasMoreElements())</span></span><span style="color: rgb(255, 0, 0);">
- System.out.print</span><span style="font-weight: bold;"><span style="color: rgb(255, 0, 0);">(enum.nextElement()+" "</span></span><span style="color: rgb(255, 0, 0);">);
- System.out.println();</span>
- //使用枚举类(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>
从例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的结果比较,可看出栈
- //先进后出的特点
- }
- }
- 运行结果(略)
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
Hashtable是Dictionary(字典)类的子类。在字典类中就把关键字对应到数据值。字典类是一个抽象类。在java.util中还有一个类Properties,它是Hashtable的子类。用它可以进行与对象属性相关的操作。
- 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
- <span style="color: rgb(255, 0, 0);"> </span><span style="font-weight: bold;"><span style="color: rgb(255, 0, 0);">Enumeration enum=v1.elements();</span></span><span style="color: rgb(255, 0, 0);">
- System.out.print("The vector v1(used method removeElement())is:");
- </span><span style="font-weight: bold;"><span style="color: rgb(255, 0, 0);">while(enum.hasMoreElements())</span></span><span style="color: rgb(255, 0, 0);">
- System.out.print</span><span style="font-weight: bold;"><span style="color: rgb(255, 0, 0);">(enum.nextElement()+" "</span></span><span style="color: rgb(255, 0, 0);">);
- System.out.println();</span>
- //使用枚举类(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.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.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.BitSet;
Java代码
- public class BitSetApp{
- private static int n=5;
- public static void main(String[] args){
- BitSet set1=new BitSet(n);
- for(int i=0;i
- //将set1的各位赋1,即各位均为true
- BitSet set2= new BitSet();
- set2=(BitSet)set1.clone();
- //set2为set1的拷贝
- set1.clear(0);
- set2.clear(2);
- //将set1的第0位set2的第2位清零
- <span style="font-weight: bold;"> System.out.println("The set1 is: "+set1);
- //直接将set1转换成字符串输出,输出的内容是set1中值true所处的位置</span>
- //打印结果为The set1 is:{1,2,3,4}
- System.out.println("The hash code of set2 is: "+set2.hashCode());
- //打印set2的hashCode
- printbit("set1",set1);
- printbit("set2",set2);
- //调用打印程序printbit(),打印对象中的每一个元素
- //打印set1的结果为The bit set1 is: false true true true true
- set1.and(set2);
- printbit("set1 and set2",set1);
- //完成set1 and set2,并打印结果
- set1.or(set2);
- printbit("set1 or set2",set1);
- //完成set1 or set2,并打印结果
- set1.xor(set2);
- printbit("set1 xor set2",set1);
- //完成set1 xor set2,并打印结果
- }
- //打印BitSet对象中的内容
- public static void printbit(String name,BitSet set){
- System.out.print("The bit "+name+" is: ");
- for(int i=0;i
- System.out.print(set.get(i)+" ");
- System.out.println();
- }
- }
-
- 运行结果:
- The set1 is: {1, 2, 3, 4}
- The hash code of set2 is: 1225
- The bit set1 is: false true true true true
- The bit set2 is: true true false true true
- The bit set1 and set2 is: false true false true true
- The bit set1 or set2 is: true true false true true
- The bit set1 xor set2 is: false false false false false
- public class BitSetApp{
- private static int n=5;
- public static void main(String[] args){
- BitSet set1=new BitSet(n);
- for(int i=0;i
- //将set1的各位赋1,即各位均为true
- BitSet set2= new BitSet();
- set2=(BitSet)set1.clone();
- //set2为set1的拷贝
- set1.clear(0);
- set2.clear(2);
- //将set1的第0位set2的第2位清零
- <span style="font-weight: bold;"> System.out.println("The set1 is: "+set1);
- //直接将set1转换成字符串输出,输出的内容是set1中值true所处的位置</span>
- //打印结果为The set1 is:{1,2,3,4}
- System.out.println("The hash code of set2 is: "+set2.hashCode());
- //打印set2的hashCode
- printbit("set1",set1);
- printbit("set2",set2);
- //调用打印程序printbit(),打印对象中的每一个元素
- //打印set1的结果为The bit set1 is: false true true true true
- set1.and(set2);
- printbit("set1 and set2",set1);
- //完成set1 and set2,并打印结果
- set1.or(set2);
- printbit("set1 or set2",set1);
- //完成set1 or set2,并打印结果
- set1.xor(set2);
- printbit("set1 xor set2",set1);
- //完成set1 xor set2,并打印结果
- }
- //打印BitSet对象中的内容
- public static void printbit(String name,BitSet set){
- System.out.print("The bit "+name+" is: ");
- for(int i=0;i
- System.out.print(set.get(i)+" ");
- System.out.println();
- }
- }
- 运行结果:
- The set1 is: {1, 2, 3, 4}
- The hash code of set2 is: 1225
- The bit set1 is: false true true true true
- The bit set2 is: true true false true true
- The bit set1 and set2 is: false true false true true
- The bit set1 or set2 is: true true false true true
- The bit set1 xor set2 is: false false false false false
程序中使用了BitSet类提供的两种构造方法:
public BitSet();
public BitSet(int n);
参数n代表所创建的BitSet类的对象的大小。BitSet类的对象的大小在必要时会由系统自动扩充。
其它方法:
public void set(int n)
将BitSet对象的第n位设置成1。
public void clear(int n)
将BitSet对象的第n位清零。
public boolean get(int n)
读取位集合对象的第n位的值,它获取的是一个布尔值。当第n位为1时,返回true;第n位为0时,返回false。
另外,如在程序中所示,当把一BitSet类的对象转换成字符串输出时,输出的内容是此对象中true所处的位置。
在BitSet中提供了一组位操作,分别是:
public void and(BitSet set)
public void or(BitSet set)
public void xor(BitSet set)
利用它们可以完成两个位集合之间的与、或、异或操作。
BitSet类中有一方法public int size()来取得位集合的大小,它的返回值与初始化时设定的位集合大小n不一样,一般为64。
1.9 Scanner类
一个可以使用正则表达式来解析基本类型和字符串的简单文本扫描器。
Scanner 使用分隔符模式将其输入分解为标记,默认情况下该分隔符模式与空白匹配。然后可以使用不同的 next 方法将得到的标记转换为不同类型的值。
例如,以下代码使用户能够从 System.in 中读取一个数:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
再看一个例子,以下代码使 long 类型可以通过 myNumbers 文件中的项分配:
Scanner sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
} 扫描器还可以使用不同于空白的分隔符。下面是从一个字符串读取若干项的例子:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
输出为:
1
2
red
blue
以下代码使用正则表达式同时解析所有的 4 个标记,并可以产生与上例相同的输出结果:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i));
s.close();