堆栈(Stack)示例
//Demonstrate the Stack class.
import java.util.*;
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.print("pop --> ");
Integer a = (Integer)st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}
public static void main(String[] args)
{
Stack st = new Stack();
System.out.println("stack: " + st);
showPush(st,9);
showPush(st,19);
showPush(st,99);
System.out.println();
showPop(st);
showPop(st);
showPop(st);
try{
showPop(st);
}catch(EmptyStackException e){
System.out.println("Empty stack");
}
}
}
Vector Enumeration 示例
//Demonstrate various vector operations.
import java.util.*;
class VectorDemo{
public static void main(String[] args)
{
//initial size is 3,increment is 2
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(9.99));
System.out.println("Current capacity :" + v.capacity());
v.addElement(new Double(77.87));
v.addElement(new Integer(9));
System.out.println("Current capacity :" + v.capacity());
v.addElement(new Float(77.7));
v.addElement(new Integer(10));
System.out.println("Current capacity :" + 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());
if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");
//enumerate the elements in the vector
Enumeration enum = v.elements();
System.out.println("Elements in vector:");
while(enum.hasMoreElements()){
System.out.print(enum.nextElement() + " ");
}
System.out.println();
/*
//use an iterator to display contents
Iterator i = v.iterator();
System.out.println("Elements in vector:");
while(i.hasNext()){
System.out.print(i.next() + " ");
}
System.out.println();
*/
}
}
Arrays类中的一些方法 示例
//Demonstrate Arrays.
import java.util.*;
class ArraysDemo{
public static void main(String[] args)
{
//allocate and initialize array
int array[] = new int[10];
for(int i = 0;i<10;i++)
array[i] = -3 * i ;
//display,sort,display
System.out.println("Original contents : ");
display(array);
Arrays.sort(array);
System.out.println("Sorted : ");
display(array);
//fill and display
Arrays.fill(array,2,6,-1);
System.out.println("After fill() : ");
display(array);
//sort and display
Arrays.sort(array);
System.out.println("After sorting again : ");
display(array);
//binary search for -9
System.out.println("The value -9 is at location : ");
int index = Arrays.binarySearch(array,-9);
System.out.println(index);
}
static void display(int array[]){
for(int i=0;i<array.length;i++){
System.out.print(array[i] + " ");
}
System.out.println();
}
}
类集算法 Collections.reverseOrder() 、 Comparator 、 shuffle()
/*
该程序创建和初始化了一个链表。reverseOrder()方法返回一个对Integer对象的比较进行逆向的Comparator函数。列表中元素按照这个比较函数进行排序并被显示出来。接下来,调用shuffle()方法对列表进行随机排列,然后显示列表的最大值和最小值。
*/
//Demonstrate various algorithms.
import java.util.*;
class AlgorithmDemo{
public static void main(String[] args)
{
//Create and initialize linked list.
LinkedList ll = new LinkedList();
ll.add(new Integer(-9));
ll.add(new Integer(10));
ll.add(new Integer(-10));
ll.add(new Integer(9));
ll.add(new Integer(99));
ll.add(new Integer(100));
//Create a reverse order comparator.
Comparator r = Collections.reverseOrder();
//Sort list by using the comparator.
Collections.sort(ll,r);
//Get an iterator.
Iterator i = ll.iterator();
System.out.println("List sorted in reverse: ");
while(i.hasNext()){
System.out.println(i.next() + " ");
}
System.out.println();
Collections.shuffle(ll);
//Display randomized list
i = ll.iterator();
System.out.print("List shuffled: ");
while(i.hasNext()){
System.out.print(i.next() + " ");
}
System.out.println();
System.out.println("Minimum: " + Collections.min(ll));
System.out.println("Maximum: " + Collections.max(ll));
}
}
TreeMap 排序
/*
比较函数类TComp比较两个包含姓和名的字符串。它首先比较姓,具体是这样做的,首先寻找每一个字符串中最后一个空格的下标,然后比较从这个位置开始的每一个元素的子字符串。当两个字符串中姓完全相等时,再比较两个名。这样就形成了先按姓排序,在姓相同的情况下再按名字进行排序的树型映射。
*/
//Use a comparator to sort accounts by last name.
import java.util.*;
//Compare last whole words in two strings.
class TComp implements Comparator{
public int compare(Object a,Object b){
int i,j,k;
String strA,strB;
strA = (String)a;
strB = (String)b;
//find index of beginning of last name
i = strA.lastIndexOf(' ');
j = strB.lastIndexOf(' ');
k = strA.substring(i).compareTo(strB.substring(j));
if(k==0) //last name match,check entire name
return strA.compareTo(strB);
else
return k;
}
//no need to override equals
}
public class TreeMapDemo2{
public static void main(String[] args)
{
//Create a tree map.
TreeMap tm = new TreeMap(new TComp());
//Put elements to the map.
tm.put("Sue Yuan",new Double(17.15));
tm.put("Jiahui Sheng",new Double(78777));
tm.put("Huajiang Chen",new Double(12345.77));
tm.put("Magic Ya",new Double(-99.10));
tm.put("Quanbing Chen",new Double(100.00));
//Get a set of the entries.
Set set = tm.entrySet();
//Get an iterator.
Iterator i = set.iterator();
//Display elements.
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.println(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
//Deposit 1000 into Jiahui Sheng's account
double balance = ((Double)tm.get("Jiahui Sheng")).doubleValue();
tm.put("Jiahui Sheng",new Double(balance + 1000));
System.out.println("Jiahui Sheng's new balance : " + tm.get("Jiahui Sheng"));
}
}
TreeSet() 类逆向排序(实现compare()方法以便按正常顺序的逆向进行操作)。
/*
仔细观察实现Comparator并覆盖compare()方法的MyComp类(覆盖equals方法既不是必须的,也不是常用的)。在compare()方法内部,String方法compareTo()比较两个字符串。然而由strB-----不是strA------调用compareTo()方法,这导致比较的结果被逆向。
*/
//Use a custom comparator
import java.util.*;
//A reverse comparator for strings.
class MyComp implements Comparator{
public int compare(Object a,Object b){
String strA,strB;
strA = (String)a;
strB = (String)b;
//reverse the comparison
return strB.compareTo(strA);
}
//no need to override equals
}
public class CompDemo{
public static void main(String[] args)
{
//Create a tree set.
TreeSet ts = new TreeSet(new MyComp());
//Ade elements to the tree set
ts.add("C");
ts.add("F");
ts.add("A");
ts.add("B");
ts.add("D");
//Get an iterator
Iterator i = ts.iterator();
//Display elements
while(i.hasNext()){
Object element = i.next();
System.out.println(element + " ");
}
System.out.println();
}
}
TreeMap用法 示例
*
TreeMap类通过使用树来实现Map接口.TreeMap提供了按排序顺序存储关键字/值对的有效手段,同时允许快速检索。不像散列映射,树映射保证它的元素按照关键字升序排序。
*/
import java.util.*;
class TreeMapDemo{
public static void main(String[] args)
{
//Creat a tree map
TreeMap tm = new TreeMap();
//Put elements to the map
tm.put("Evan",new Double(12345.77));
tm.put("Rose",new Double(78777));
tm.put("Magic",new Double(-99.10));
tm.put("Mike",new Double(100.00));
tm.put("Sue",new Double(17.15));
//Get a set of entries
Set set = tm.entrySet();
//Get an iterator
Iterator i = set.iterator();
//Display elements
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.println(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
//Deposit 1000 into Evan's account
double balance = ((Double)tm.get("Evan")).doubleValue();
tm.put("Evan",new Double(balance + 1000));
System.out.println("Evan's new balance : " + tm.get("Evan"));
}
}
HashMap用法 示例
/*
程序开始创建了一个散列映射,然后将名字的映射增加到平衡表。接下来,映射的内容通过使用由调用函数entrySet()而获得的集合“视图”而显示出来。关键字和值通过调用由Map.Entry定义的getKey()和getValue()方法而显示。注意存款是如何被制成Evan的账目的。put()方法自动用新值替换与指定关键字相关联的原先值。
*/
import java.util.*;
class HaspMapDemo{
public static void main(String[] args)
{
//Create a hasp map
HashMap hm=new HashMap();
//Put elements to the map
hm.put("Evan",new Double(12345.77));
hm.put("Rose",new Double(78777));
hm.put("Magic",new Double(-99.10));
hm.put("Mike",new Double(100.00));
hm.put("Sue",new Double(17.15));
//Get a set of the entries
Set set = hm.entrySet();
//Get an iterator
Iterator itr = set.iterator();
//Display elements
while (itr.hasNext()){
Map.Entry me = (Map.Entry)itr.next();
System.out.println(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
//Deposit 1000 into Evan's account
double balance = ((Double)hm.get("Evan")).doubleValue();
hm.put("Evan",new Double(balance + 1000));
System.out.println("Evan's new balance : " + hm.get("Evan"));
}
}
使用LinkedList存储信箱地址
/*
除了在类集中存储用户定义的类之外,关于下面程序的另一个重要的、值得注意的事情是它非常短.当考虑用50行代码建立一个能实现存储、检索以及处理信息地址的链表时,类集框架的能力就变得显而易见了.
*/
//A simple mailing list example.
import java.util.*;
class Address{
private String name;
private String street;
private String city;
private String state;
private String code;
Address(String n,String str,String c,String sta,String cod){
name = n;
street = str;
city = c;
state = sta;
code = cod;
}
public String toString(){
return name +"/n"+ street +"/n"+
city +" "+ state +" "+ code;
}
}
public class MailList{
public static void main(String[] args)
{
LinkedList ll = new LinkedList();
//Add elements to the linked list
ll.add(new Address("ChenHuajiang","11 Oka Ave","Urbana","IL","61801"));
ll.add(new Address("ShengJiahui","12 Oka Ave","Urbana","IL","61802"));
ll.add(new Address("Magic","13 Oka Ave","Urbana","IL","61803"));
Iterator itr = ll.iterator();
while(itr.hasNext()){
Object element = itr.next();
System.out.println(element +"/n");
}
System.out.println();
}
}
使用TreeSet()进行排序
/*
TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储.访问和检索是很快的。在存储了大量的需要进行快速检索的排序信息的情况下,TreeSet是一个很好的选择.
*/
//Demonstrate TreeSet
import java.util.*;
class TreeSetDemo
{
public static void main(String[] args)
{
//Create a TreeSet
TreeSet ts = new TreeSet();
ts.add("C");
ts.add("A");
ts.add("B");
ts.add("E");
ts.add("F");
System.out.println(ts);
}
}