Java数据结构

来源:互联网 发布:mac子弹头 知乎 编辑:程序博客网 时间:2024/06/05 17:42

更多参考:http://www.yiibai.com/java/java_data_structures.html

Vector - 矢量

import java.util.Enumeration;import java.util.Vector;public class VectorDemo {public static void main(String[] args) {Vector v = new Vector(3,2);System.out.println("Initial size:"+v.size());System.out.println("Initial capacity:"+v.capacity());v.addElement(new Integer(1));v.addElement(new Integer(2));v.addElement(new Integer(3));v.addElement(new Integer(4));System.out.println("Capacity after four additions: " + v.capacity());v.addElement(new Double(5.45));System.out.println("Current capactity:"+v.capacity());v.addElement(new Double(6.08));v.addElement(new Integer(7));System.out.println("Current capactity:"+v.capacity());v.addElement(new Integer(11));v.addElement(new Integer(12));System.out.println("First element: " +(Integer)v.firstElement());System.out.println("Last element: " +         (Integer)v.lastElement());System.out.println("Current size:"+v.size());if(v.contains(new Integer(3)))         System.out.println("Vector contains 3.");Enumeration enumeration = v.elements();System.out.println("Elements in vector:");while(enumeration.hasMoreElements()){System.out.println(enumeration.nextElement()+"");}System.out.println();}}

结果:

Initial size:0
Initial capacity:3
Capacity after four additions: 5
Current capactity:5
Current capactity:7
First element: 1
Last element: 12
Current size:9
Vector contains 3.
Elements in vector:
1
2
3
4
5.45
6.08
7
11
12

capacity的计算问题参考:http://wenda.so.com/q/1407928700720046

本例中中的Capacity初始化容量是3,单容量超过3。就以2个的扩展。因为Vector v = new Vector(3,2);


Stack - 堆栈

import java.util.EmptyStackException;import java.util.Stack;public class StackDemo {static void showpush(Stack st, int a) {st.push(new Integer(a));System.out.println("push(" + a + ")");System.out.println("stack:" + st);}static void showpop(Stack st) {System.out.println("pop-->");Integer a = (Integer) st.pop();System.out.println(a);System.out.println("stack:" + st);}public static void main(String[] args) {Stack stack = new Stack();System.out.println("stack:" + stack);showpush(stack, 42);showpush(stack, 66);showpush(stack, 99);showpop(stack);showpop(stack);showpop(stack);try {showpop(stack);} catch (EmptyStackException e) {System.out.println("empty stack");}}}
结果:

stack:[]push(42)stack:[42]push(66)stack:[42, 66]push(99)stack:[42, 66, 99]pop-->99stack:[42, 66]pop-->66stack:[42]pop-->42stack:[]pop-->empty stack

Hashtable

import java.util.Enumeration;import java.util.Hashtable;public class HashTableDemo {public static void main(String[] args) {Hashtable balance = new Hashtable();Enumeration names;String str;double bal;balance.put("Zara", new Double(3434.34));balance.put("Mahnaz", new Double(123.22));balance.put("Ayan", new Double(1378.00));balance.put("Daisy", new Double(99.22));balance.put("Qadir", new Double(-19.08));names = balance.keys();while(names.hasMoreElements()){str = (String) names.nextElement();System.out.println(str+",balance:"+balance.get(str));}System.out.println();bal = ((Double)balance.get("Zara")).doubleValue();balance.put("Zara", new Double(bal+1000));System.out.println("Zara's new balance: "+balance.get("Zara"));}}

结果:

Qadir,balance:-19.08Zara,balance:3434.34Mahnaz,balance:123.22Daisy,balance:99.22Ayan,balance:1378.0Zara's new balance: 4434.34

Properties

import java.util.Iterator;import java.util.Properties;import java.util.Set;public class PropDemo {public static void main(String[] args) {Properties capitals = new Properties();Set states;String str;capitals.put("Illinois", "Springfield");capitals.put("Missouri", "Jefferson City");capitals.put("Washington", "Olympia");capitals.put("California", "Sacramento");capitals.put("Indiana", "Indianapolis");states = capitals.keySet();Iterator itr = states.iterator();while(itr.hasNext()){str = (String) itr.next();System.out.println("The capital of " +str + " is " + capitals.getProperty(str) + ".");}System.out.println();str = capitals.getProperty("Florida", "no found");System.out.println("The capital of California  is " + str);}}

结果:

The capital of Missouri is Jefferson City.The capital of Illinois is Springfield.The capital of Indiana is Indianapolis.The capital of California is Sacramento.The capital of Washington is Olympia.The capital of California  is no found



0 0