您的位置:首页 > 其它

IBM项目组的一个上机题目(离散事件处理)Collection用法

2007-04-17 10:46 281 查看
在我写的这个例子中,有3个类。collectiontool,listtool,things。都在包package collectiontools中。

下面是类的源码:

第一个类:collectiontool

package collectiontools;
import collectiontools.listtool;
import java.lang.Exception;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import collectiontools.things;
import java.util.Calendar;
import java.lang.Thread;
import java.util.Iterator;

/**
* 这个例子是我在参加IBM一个项目组的面试的过程中的一个上机题。当时对java.util.Collection
* 不是很了解,而且它的题目的说明不是很清晰,好像是甚么离散事件处理机制。搞的我非常头大,当时
* 没有写出来,回来看了看javaAPI中关于Collection和Collections的介绍后,写下了以下的类和
* 测试信息。感觉大体上差不多,就拿出来晒晒,大家多指正。
* 主要是完成一个事件处理功能。允许添加事件,其中事件必须自带自己的加入时间;
* 事件在处理完毕后,自动从事件组中删除;事件在加入事件组后,必须进行排序;
* 允许在处理事件组中事件的过程中,加入一个新的事件;需要有删除最后一个事件的功能。
* @author sw
*
*/
public class collectiontool {
public static void main(String args[])throws Exception{
listtool iList = new listtool();
iList.add("aaa");
Thread.sleep(1000);
iList.add("abc");
Thread.sleep(1000);
iList.add("bac");
Thread.sleep(1000);
iList.add("aac");
Thread.sleep(1000);
iList.add("efd");
iList.sort();
System.out.print(iList.toString());
for(int i=0 ;i<iList.size();i++){
System.out.println(""+((things)iList.get(i)).getThing());
System.out.println(""+((things)iList.get(i)).getLDate());
}
iList.runthings("aaa");
for(int i=0 ;i<iList.size();i++){
System.out.println(""+((things)iList.get(i)).getThing());
}
iList.runandcreatethings("efd","aaa");
for(int i=0 ;i<iList.size();i++){
System.out.println(""+((things)iList.get(i)).getThing());
}
try{
iList.removethelastthing();
}catch(Exception e){
e.printStackTrace();
}
for(int i=0 ;i<iList.size();i++){
System.out.println(""+((things)iList.get(i)).getThing());
}
iList.runalll();
//以下是在没有写listtool类的时候,进行的测试工作。
// List iList = new ArrayList();
// iList.add(new things(Calendar.getInstance().getTime(),"aaa"));
// Thread.sleep(1000);
// iList.add(new things(Calendar.getInstance().getTime(),"bbb"));
// Thread.sleep(1000);
// iList.add(new things(Calendar.getInstance().getTime(),"abc"));
// Thread.sleep(1000);
// iList.add(new things(Calendar.getInstance().getTime(),"efd"));
// Collections.sort(iList,new Comparator(){
// public int compare(Object o1,Object o2){
// things t1 = (things)o1 ;
// things t2 = (things)o2 ;
// if(t1.getLDate().before(t2.getLDate())){
// return 1 ;
// }else{
// return -1 ;
// }
//
// }
// });
// for(int i=0 ;i<iList.size();i++){
// System.out.println(""+((things)iList.get(i)).getThing());
// System.out.println(""+((things)iList.get(i)).getLDate());
// }
// Iterator it = iList.iterator();
// while(it.hasNext()){
// System.out.println(""+((things)it.next()).getThing());
// }
}
}

第二个类:listtool

package collectiontools;
import java.util.Calendar;
import java.util.Date ;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import collectiontools.things;
/**
* 定义了一个List,实现其添加,删除,排序等的功能
* @author sw
*
*/
public class listtool {
private List iList ;
//构造函数
public listtool() {
this.iList = new ArrayList();
}
//添加事件
public void add(String s){
things t = new things(Calendar.getInstance().getTime(),s);
this.iList.add(t);
}
//返回list长度
public int size(){
return this.iList.size();
}
//获取事件
public Object get(int index){
return this.iList.get(index);
}
//list的排序
public void sort(){
if(this.iList.size() < 2){
System.out.println("只有一个事件,无需排序!");
return ;
}
Collections.sort(this.iList,new Comparator(){
public int compare(Object o1,Object o2){
things t1 =(things)o1 ;
things t2 =(things)o2 ;
if(t1.getLDate().before(t2.getLDate())){
return 1 ;
}else{
return -1 ;
}
}
});
}
//list中的事件运行,运行结束后自动从list中删除
public void runthings(String s){
Iterator it = this.iList.iterator();
while(it.hasNext()){
String st = ((things)it.next()).getThing();
if(st.equals(s)){
System.out.println("事件"+st+"运行......");
it.remove();
System.out.println("事件"+st+"运行完毕。");
}
}
}
//在list运行事件的过程中,允许添加一个额外的事件。
public void runandcreatethings(String s,String ans) throws Exception{
Iterator it = this.iList.iterator();
while(it.hasNext()){
String st = ((things)it.next()).getThing();
if(st.equals(s)){
System.out.println("事件"+st+"运行......");
it.remove();
System.out.println("事件"+st+"运行完毕。");
}
}
System.out.println("事件"+ans+"将要添加......");
Thread.sleep(1000);
this.add(ans);
System.out.println("事件"+ans+"添加完毕。");
this.sort();
}
//运行list中的所有事件
public void runalll(){
Iterator it = this.iList.iterator();
while(it.hasNext()){
String st = ((things)it.next()).getThing();
System.out.println("事件"+st+"运行......");
it.remove();
System.out.println("事件"+st+"运行完毕。");
}
}
//删除list中最后一个添加的事件
public void removethelastthing(){
things t = (things)this.get(this.size()-1);
String st = t.getThing();
System.out.println("事件"+st+"删除......");
this.iList.remove(t);
System.out.println("事件"+st+"删除完毕。");

}
//输出事件信息
public String toString(){
StringBuffer s = new StringBuffer() ;
Iterator it = this.iList.iterator();
while(it.hasNext()){
things t = (things)it.next() ;
String st = t.getThing();
s.append(st);
s.append(" ");
java.util.Date ldt =t.getLDate();
s.append(DatetoString(ldt));
s.append("/r/n");
}
return s.toString();
}
//把事件的事件以字符串的形式输出。
private String DatetoString(Date date){
StringBuffer s = new StringBuffer();
s.append("");
Calendar cal = Calendar.getInstance();
cal.setTime(date);
s.append(cal.get(cal.YEAR));
s.append("年");
s.append(cal.get(cal.MONTH));
s.append("月");
s.append(cal.get(cal.DAY_OF_MONTH));
s.append("日 ");
s.append(IntToString(cal.get(cal.HOUR)));
s.append(":");
s.append(IntToString(cal.get(cal.MINUTE)));
s.append(":");
s.append(IntToString(cal.get(cal.SECOND)));
s.append(" ");
return s.toString() ;
}
private String IntToString(int i){
String str = new String();
str = "00"+i;
str = str.substring(str.length()-2);
return str ;
}
}

第三个类:things

package collectiontools;

import java.util.Date;

/**
* 定义事件
* @author sw
*
*/
public class things {
private Date LDate ;
private String LThing ;
public things(Date ldate,String lthing){
this.LDate = ldate;
this.LThing = lthing ;
}
public void setLDate(Date ldate){
this.LDate = ldate ;
}
public Date getLDate(){
return this.LDate;
}
public String getThing (){
return this.LThing;
}
public void setThing(String thing){
this.LThing = thing ;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐