⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8.txt

📁 电子工业出版社出版的《java2应用开发指南》配套光盘源代码
💻 TXT
字号:
例程8-1
//CollectionDemo.java
import java.util.*;

public class CollectionDemo
{
	public static void main( String[] args )
	{
		String strMonths[]={
			"一月","二月","三月",
			"四月","五月","六月",
			"七月","八月","九月",
			"十月","十一月","十二月"
		};
		//获得月份的数目
		int nMonthLen= strMonths.length;
		//创建 arrayList 对象
		Collection months= new ArrayList();
		//pub months value to arraylist object
		for( int i=0; i< nMonthLen; i++){
			months.add(strMonths[i]);
		}
		/打印arraylist中的所有元素
		Object s[] = months.toArray();
		for( int i=0; i< s.length; i++ ){
			System.out.println(s[i].toString());
		}
	}
}



例程8-2
//ListDemo.java
import java.util.*;

public class ListDemo
{
	public static void main( String[] args )
	{
		String strMonths[]={
			"一月","二月","三月",
			"四月","五月","六月",
			"七月","八月","九月",
			"十月","十一月","十二月"
		};
		//get count of months
		int nMonthLen= strMonths.length;
		//create arrayList object
		List months= new ArrayList();
		//pub months value to arraylist object
		for( int i=0; i< nMonthLen; i++){
			months.add(strMonths[i]);
		}
		//print all element in arraylist
		for( int i=months.size()-1; i>=0;  i-- ){
			System.out.println(months.get(i));
		}
	}
}
在该文件所在的目录下输入下列命令来编译和运行:
javac  	ListDemo.java
java		ListDemo
下面是运行的输出结果:
十二月
十一月
十月
九月
八月
七月
六月
五月
四月
三月
二月
一月



例程8-3
//MapDemo.java
import java.util.*;
public class MapDemo
{
	public static void main( String[] args )
	{
		String strMonths[]={
			"一月","二月","三月",
			"四月","五月","六月",
			"七月","八月","九月",
			"十月","十月","十二月"
		};
		String strVaules[]={
			"学习Java2应用开发指南中的第一章",
			"学习Java2应用开发指南中的第二章",
			"学习Java2应用开发指南中的第三章",
			"学习Java2应用开发指南中的第四章",
			"学习Java2应用开发指南中的第五章",
			"学习Java2应用开发指南中的第六章",
			"学习Java2应用开发指南中的第七章",
			"学习Java2应用开发指南中的第八章",
			"学习Java2应用开发指南中的第九章",
			"学习Java2应用开发指南中的第十章",
			"学习Java2应用开发指南中的第十一章",
			"学习Java2应用开发指南中的第十二章"
		};
		//get count of months(Key)
		int nMonthLen= strMonths.length;
		//create TreeMap object
		Map map= new TreeMap();
		//put key and value to TreeMap object
		for( int i=0; i< nMonthLen; i++){
			map.put(strMonths[i],strVaules[i]);
		}
		//print all keys and values in TreeMap
		for( int i=0; i< map.size() ; i++ ){
			//get Key
			Object strKey=map.keySet().toArray()[i];
			System.out.println(strKey+ ":  " + map.get(strKey));
		}
	}
}



例程8-4
//SetDemo.java
import java.util.*;
public class SetDemo
{
	public static void main( String[] args )
	{
		String strMonths[]={
			"一月","二月","三月",
			"四月","五月","六月",
			"七月","八月","九月",
			"十月","十月","十二月"
		};
		//get count of months(Key)
		int nMonthLen= strMonths.length;
		//create HashSet object
		Set set= new HashSet();
		//put element into HashSet object
		for( int i=0; i< 100; i++){
			int index=(int)(Math.random()*nMonthLen);
			set.add(strMonths[index]);
		}
		//print all element value in HashSet
		//get Iterator
		Iterator it=set.iterator();
		while( it.hasNext()){
			System.out.println(it.next());
		}
	}
}



例程8-5
//LinkDemo.java
import java.io.*;
public class LinkDemo 
{
	/*构成链表的结点定义*/
	class Node
	{
		Object data;
		Node next;
	
		Node(Object d)
		{
			data=d;
			next=null;
		}
	}
	//用变量来实现表头
	private Node Head=null;
	private Node Tail=null;
	private Node Pointer=null;
	private int Length=0;
	/*清空整个链表*/
	public void deleteAll()
	{
		Head=null;	
		Tail=null;	
		Pointer=null;	
		Length=0;
	}
	//链表复位,使第一个结点成为当前结点
	public void reset()
	{
		Pointer=null;
	}
	/*判断链表是否为空*/
	public boolean isEmpty()
	{
		return(Length==0);
	}
	/*判断当前结点是否为最后一个结点*/
	public boolean isEnd()
	{
		if(Length==0)
			throw new java.lang.NullPointerException();
		else if(Length==1)	
			return true;
		else	
			return(cursor()==Tail);
	}
	//返回当前结点的下一个结点的值,并使其成为当前结点
	public Object nextNode()
	{
		if(Length==1)	
			throw new java.util.NoSuchElementException();
		else if(Length==0)
			throw new java.lang.NullPointerException();
		else
		{
			Node temp=cursor();
			Pointer=temp;
			if(temp!=Tail)
				return(temp.next.data);
			else
				throw new java.util.NoSuchElementException();
		}
	}
	//返回当前结点的值
	public Object currentNode()
	{
		Node temp=cursor();
		return temp.data;
	}
	/*在当前结点前插入一个结点,并使其成为当前结点*/
	public void insert(Object d)
	{
		Node e=new Node(d);
		if(Length==0)
		{
			Tail=e;
			Head=e;
		}
		else
		{
			Node temp=cursor();
			e.next=temp;
			if(Pointer==null)
				Head=e;
			else
			Pointer.next=e;
		}
		Length++;
	}
	//返回链表的大小
	public int size()
	{
		return (Length);
	}
	/*	将当前结点移出链表,下一个结点成为当前结点,如果移出的结点
是最后一个结点,则第一个结点成为当前结点
*/
	public Object remove()
	{
		Object temp;
		if(Length==0)
			throw new java.util.NoSuchElementException();	
		else if(Length==1)
		{
			temp=Head.data;
			deleteAll();
		}
		else
		{	
			Node cur=cursor();	
			temp=cur.data;		
			if(cur==Head)
				Head=cur.next;
			else if(cur==Tail)
			{	
				Pointer.next=null;	
				Tail=Pointer;	
				reset();
			}	
			else
				Pointer.next=cur.next;	
			Length--;	
		}	
		return temp;
	}
	/*返回当前结点的指针*/
	private Node cursor()
	{
		if(Head==null)	
			throw new java.lang.NullPointerException();	
		else if(Pointer==null)	
			return Head;	
		else	
		return Pointer.next;
	}
	/*链表的简单应用举例*/
	public static void main(String[] args)
	{
		//构造链表并创建10给节点
		LinkDemo a=new LinkDemo ();
		for(int i=1;i<=10;i++)
			a.insert(new Integer(i));
		//使链表复位
		a.reset();
		//输出链表的节点
		for( int j=1; j< a.Length; j++)
			System.out.println(a.nextNode());
	}
}



例程8-6
//SortDemo.java
import java.util.*;
public class SortDemo
{
	//实现Comparator接口的静态类
	static class Comp implements Comparator{
		public int compare( Object obj1, Object obj2){
			String s1=obj1.toString().toLowerCase();
			String s2=obj2.toString().toLowerCase();
			return s1.compareTo(s2);
		}
	}
	//程序的入口方法
	public static void main( String[] args )
	{
		String strMonths[]={
			"One","Two","three",
			"Four","five","Six",
			"Sevent","eight","Night",
			"Ten","eleven","Twelve"
		};
		//利用Arrays类进行排序
		Arrays.sort(strMonths);
		//get count of months
		int nMonthLen= strMonths.length;
		//按照字母顺序进行排序
		for( int i=0; i< nMonthLen; i++ ){
			System.out.print(strMonths[i] + "  ");
		}
		//空行
		System.out.println();
		//用比较器进行构造
		Arrays.sort(strMonths,new Comp());
		for( int i=0; i< nMonthLen; i++ ){
			System.out.print(strMonths[i] + "  ");
		}
	}
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -