BlogJava-も智军ミ 心系java-随笔分类-java容器类http://www.blogjava.net/zj474097500/category/46945.html Java的魅力:跨平台、动态的Web、Internet计算 Java在Web、移动设备以及云计算方面前景广阔 我的java编程不是梦!!!zh-cnSat, 06 Nov 2010 17:26:13 GMTSat, 06 Nov 2010 17:26:13 GMT60java容器类的层次及区别http://www.blogjava.net/zj474097500/archive/2010/11/07/337435.htmlセ智军ミセ智军ミSat, 06 Nov 2010 17:08:00 GMThttp://www.blogjava.net/zj474097500/archive/2010/11/07/337435.htmlhttp://www.blogjava.net/zj474097500/comments/337435.htmlhttp://www.blogjava.net/zj474097500/archive/2010/11/07/337435.html#Feedback0http://www.blogjava.net/zj474097500/comments/commentRss/337435.htmlhttp://www.blogjava.net/zj474097500/services/trackbacks/337435.html    
一、容器类层次




二、容器类的区别       
                                                                 

    1)、Vector和ArrayList  
                1,vector是线程同步的,所以它也是线程安全的,而arraylist是线程异步的,是不安全的。如果不考虑到线程的安全因素,一般用arraylist效率比较高。
                 2,如果集合中的元素的数目大于目前集合数组的长度时,vector增长率为目前数组长度的100%,而arraylist增长率为目前数组长度的50%.如过在集合中使用数据量比较大的数据,用vector有一定的优势。
 
             ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动等内存操作,所以索引数据快插入数据慢,Vector由于使synchronized方法(线程安全)所以性能上比ArrayList要差,
             LinkedList使用双向链表实现存储,按序号索引数据需要进行向前或向后遍历,但是插入数据时只需要记录本项的前后项即可,所以插入数度较快

     2)、arraylist和linkedlist

     1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。
     2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。
     3.对于新增和删除操作add和remove,LinkedList比较占优势,因为ArrayList要移动数据。
       这一点要看实际情况的。若只对单条数据插入或删除,ArrayList的速度反而优于LinkedList。
       但若是批量随机的插入删除数据,LinkedList的速度大大优于ArrayList. 因为ArrayList每插入一条数据,要移动插入点及之后的所有数据。


    3)、HashMap与TreeMap 

             1、HashMap通过hashcode对其内容进行快速查找,而TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用TreeMap(HashMap中元素的排列顺序是不固定的)。集合框架”提供两种常规的Map实现:HashMap和TreeMap (TreeMap实现SortedMap接口)。

            2、在Map 中插入、删除和定位元素,HashMap 是最好的选择。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。使用HashMap要求添加的键类明确定义了hashCode()和 equals()的实现。  

这个TreeMap没有调优选项,因为该树总处于平衡状态。

   4)、hashtable与hashmap

     1.历史原因:Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现

     2.同步性:Hashtable是线程安全的,也就是说是同步的,而HashMap是线程序不安全的,不是同步

     3.值:只有HashMap可以让你将空值作为一个表的条目的key或value

    5)、Set与List
       Set中的数据对象没有顺序不可以重复
       List中的数据对象有顺序可以重复



セ智军ミ 2010-11-07 01:08 发表评论
]]>
病人挂号等待,各类医生专家按顺序给排队病人依次作诊的UI小程序http://www.blogjava.net/zj474097500/archive/2010/11/03/337165.htmlセ智军ミセ智军ミWed, 03 Nov 2010 09:58:00 GMThttp://www.blogjava.net/zj474097500/archive/2010/11/03/337165.htmlhttp://www.blogjava.net/zj474097500/comments/337165.htmlhttp://www.blogjava.net/zj474097500/archive/2010/11/03/337165.html#Feedback0http://www.blogjava.net/zj474097500/comments/commentRss/337165.htmlhttp://www.blogjava.net/zj474097500/services/trackbacks/337165.htmlnum),一个是前面需要等待的人数(frontnum);
                            

package com.dr.patient;

public class Patient {
      private int num;    
   private int frontNum;
  
 public int getNum() {
  return num;
 }
 public void setNum(int num) {
  this.num = num;
 }
 public int getFrontNum() {
  return frontNum;
 }
 public void setFrontNum(int frontNum) {
  this.frontNum = frontNum;
 }

                                                                                                                    }

          2)、然后建立一个病人服务的类(PatientServer),构建一个病人等待排队数字的循环逻辑;
package com.dr.patient;

import java.util.LinkedList;
import java.util.Queue;

public class PatientServer {
     Queue<Patient> patientList = new LinkedList<Patient>();                //新建一个容器,病人的队列
   
 public Queue<Patient> init(){                                                            //构建一个方法,把挂号的病人加入
  
  for(int i=1;i<=24;i++){                                                               //设定队列中已有24个人,挂号加入的从第25个开始
   Patient pat = new Patient();
      pat.setNum(i);
      pat.setFrontNum(i-1);
      patientList.offer(pat);                                                     //将指定的病人元素插入此队列
  }
  return patientList;
}
}


      3)、病人挂号点击页面,主要用Display、Shell方法新建窗口!

final Display display = Display.getDefault();
  final Shell shell = new Shell();
  shell.setBounds(300, 100, 800, 700);                                              
  shell.setText("病人挂号排队端");




// ------------------新插入的界面核心代码------------------------
  PatientServer qs = new PatientServer();//实例化类
  final Queue<Patient> patientList = qs.init();//初始化队列服务器
  
  final Text txt = new Text(shell,SWT.MULTI);                                          //建立窗口里文本的的大小
     txt.setBounds(150, 50, 500, 450);

     final Button button = new Button(shell, SWT.Activate);                         //设置一个button按钮
                                                                                                  

 button.addSelectionListener(new SelectionAdapter() {                       //鼠标单击按钮事件
          public void widgetSelected(SelectionEvent e) {
                Patient p = new Patient();
                p.setNum(patientList.size()+1);
                 p.setFrontNum(patientList.size());
                 patientList.offer(p);
              if(patientList.size() <= 48){
                          txt.setText( "您好!您现在排在"+p.getNum()+"位置上,\n\n您前面有"+p.getFrontNum()+"个病人需要看病\n\n请您耐心等候吧!\n\n\n\n若您为急诊,则直接去急诊室看病就可以了!");
    }else{
     txt.setText("您现在排在"+p.getNum()+"位置上,\n\n\n您前面已经超过36个病人等待了,请您尽快选择换地吧!给你带来的不便,望您见谅!\n");
    }
   }
  });
  
  button.setBounds(300, 530, 200, 75);                                                       / 设置按钮位置
  button.setFont(new Font(display,"华文楷体",12,SWT.BOLD));
  button.setText("生病挂号");                                                                   // 设置按钮上的文字
    

  shell.layout();                                                  
  shell.open();                                                                            //打开窗口
  while (!shell.isDisposed()) {                                                    //支撑窗口在不点击任何按钮时一直保留在桌面上
   if (!display.readAndDispatch())
    display.sleep();
  }
 }
}




4)、再封装一个急救病人的类(PatientHuarry)!方法和封装病人的类基本一样。

package com.dr.patient;

public class PatientHuarry {
  private int num;                                     //两个急求病人的属性
   private int frontNum;
 public int getNum() {
  return num;
 }
 public void setNum(int num) {
  this.num = num;
 }
 public int getFrontNum() {
  return frontNum;
 }
 public void setFrontNum(int frontNum) {
  this.frontNum = frontNum;
 }
}



        5)、再从新构建一个新的队列(patienthuarryList),让其承载急救病人,而进入队列的方法思维也是和Patient的类一样

public class PatientServerH {
 Queue<PatientHuarry> patienthuarryList = new LinkedList<PatientHuarry>();

 public Queue<PatientHuarry> init(){
  
  for(int i=1;i<=16;i++){                                    //这对列能放的人数
   PatientHuarry ph = new PatientHuarry();
      ph.setNum(i);
      ph.setFrontNum(i-1);
      patienthuarryList.offer(ph);
  }
  return patienthuarryList;
}
}


    6)、最后构建医生专家按病人挂号排序就诊的页面!而为急诊的病人,则不需挂号,直接去急诊室就诊。
          医生专家的UI界面:




public class DoctorUI {
      public static void main(String[] args) {
  
        final Display display = Display.getDefault();
        final Shell shell = new Shell();
        shell.setBounds(200, 150, 1000, 600);
        shell.setText("医生专家服务端");
  
  // ------------------新插入的界面核心代码------------------------
  PatientServer qs = new PatientServer();                          //实例化类
     final Queue<Patient> patientList = qs.init();                  //初始化队列服务器
     
     PatientServerH qsh = new PatientServerH();
     final Queue<PatientHuarry> patienthuarryList = qsh.init();
    
     final Text txt1 = new Text(shell,SWT.MULTI);                        //创建四个文本窗口
     txt1.setBounds(80, 50, 170, 300);
  
     final Text txt2 = new Text(shell,SWT.MULTI);
     txt2.setBounds(300, 50, 170, 300);
  
     final Text txt3 = new Text(shell,SWT.MULTI);
     txt3.setBounds(520, 50, 170, 300);
  
     final Text txt4 = new Text(shell,SWT.MULTI);
     txt4.setBounds(740, 50, 170, 300);

     // 事件代码里要访问button
     final Button button1 = new Button(shell, SWT.Activate);                         // button1的事件方法
     button1.addSelectionListener(new SelectionAdapter() {                       // 加一个选择监听器
     public void widgetSelected(SelectionEvent e) {
    
      Patient p= patientList.poll();                      //弹出一个队列中的病人
    
          if(p!=null){
                txt1.setText(p.getNum()+"号病人请到内科专家处看病\n\n我们部门在一层左拐第一间,\n很乐意为您看病!!\n\n\n\n\n\n\n\n您好!请按时吃药!");
          }
        else{
               txt1.setText("现在没有等候的病人了,\n可以喝杯水了哈");
        }       
     
  
   }
     });
      button1.setBounds(120, 400, 90, 40);                 // 设置按钮位置
      button1.setText("内科专家");                            // 设置按钮上的文字
  

   final Button button2 = new Button(shell, SWT.Activate);                        // button2的事件方法

   button2.addSelectionListener(new SelectionAdapter() {
       public void widgetSelected(SelectionEvent e) {
        Patient p= patientList.poll();
        if(p!=null){
           txt2.setText(p.getNum()+"号病人请到外科专家处看病\n\n我们部门在二层右拐第三间,\n很乐意为您看病!!\n\n\n\n\n\n\n\n您好!请慢走!");
         }
         else{
          txt2.setText("现在没有等候的病人了,\n可以喝杯水了哈");
         }       
    }
      });
      button2.setBounds(340, 400, 90, 40);
      button2.setText("外科专家");
    
    final Button button3 = new Button(shell, SWT.Activate);                         // button3的事件方法

    button3.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
      
     Patient p= patientList.poll();
     if(p!=null){
          txt3.setText(p.getNum()+"号病人请到放射科科专家处看病\n\n我们部门在一层右拐最里一间,\n很乐意为您看病!!\n\n\n\n\n\n\n\n您好!请注意调理!");
         }
         else{
          txt3.setText("现在没有等候的病人了,\n可以喝杯水了哈!");
         }      
      
      
    
     }
       });
       button3.setBounds(560, 400, 90, 40); 
       button3.setText("放射科专家");

     
     final Button button4 = new Button(shell, SWT.Activate);                       // button4的事件方法

     button4.addSelectionListener(new SelectionAdapter() {
     public void widgetSelected(SelectionEvent e) {
       
      PatientHuarry ph= patienthuarryList.poll();
      if(ph!=null){
           txt4.setText(ph.getNum()+"号急诊病人请到急诊专家处看病\n我们部门在一层左拐第三间,\n很乐意为您看病!!\n\n\n\n由于的您病情比较着急,\n我们会全力为您治疗,\n请您放心!!\n\n\n\n\n\n\n\n您好!请保重身体!");
          }
          else{
           txt4.setText("现在没有急诊的病人了,\n我们可以休息会儿了!\n嘻嘻!");
          }      
       
     
      }
        });
        button4.setBounds(780, 400, 90,40);
        button4.setText("急诊专家");
    
    
  shell.layout();
  shell.open();
  while (!shell.isDisposed()) {                                   //同样为支撑窗口在不点击任何按钮时一直保留在桌面上
   if (!display.readAndDispatch())  
    display.sleep();
  }
    }
}



总结:这小程序主要是根据医院服务需求,做了简单的病人看病需要挂号,而医生作诊同样需要呼叫病人,这就无形中减少了病人和医生之间话费时间的联系!
             在编辑这个程序的过程中,主要还是用到的一些窗口的控件及其方法。




     最后也希望在看后我程序的人,给与我点评!我想从大家的想法中学到更多的东西,谢谢!  















セ智军ミ 2010-11-03 17:58 发表评论
]]>