Java数据结构

更多参考: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-->
99
stack:[42, 66]
pop-->
66
stack:[42]
pop-->
42
stack:[]
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.08
Zara,balance:3434.34
Mahnaz,balance:123.22
Daisy,balance:99.22
Ayan,balance:1378.0

Zara'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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值