现在的位置: 首页 > 综合 > 正文

写着玩的练习1

2013年10月02日 ⁄ 综合 ⁄ 共 2588字 ⁄ 字号 评论关闭

1.冒泡

[java] view
plain
copy

  1. static void sort(int[] arry)  
  2. {  
  3.   
  4.     for(int i=0;i<arry.length-1;i++)  
  5.     {         
  6.         //System.out.println("lentgh=>"+arry.length);      
  7.         if(arry[i]<arry[i+1])  
  8.         {  
  9.             int temp=0;  
  10.             //System.out.println("[i]=>"+arry[i]);  
  11.             //System.out.println("[i]+1=>"+arry[i+1]);  
  12.             temp=arry[i];  
  13.             arry[i]=arry[i+1];  
  14.             arry[i+1]=temp;  
  15.               
  16.             i=-1;//从新检查排序, 自加后归零  
  17.         }  
  18.           
  19.     }  
  20.   
  21. }  



2.按规定的个数,给字符串截取成若干组

[java] view
plain
copy

  1. String test="123156498478645614651";  
  2. for(int i=0;i<test.length();i+=5)  
  3. {  
  4.     System.out.println(test.substring(i, ( i+5 >test.length() ? test.length() :i+5 )));  
  5. }  


3.生产者消费者

[java] view
plain
copy

  1. public class Per_Cer {  
  2.       
  3.     public static void main(String[] args) {  
  4.           
  5.         CangKu ck=new CangKu(5);  
  6.         P p =new P(ck);  
  7.         C c =new C(ck);  
  8.           
  9.         Thread t1=new Thread(p);  
  10.         Thread t2=new Thread(c);  
  11.           
  12.         t1.start();  
  13.         t2.start();  
  14.     }  
  15.   
  16. }  
  17.   
  18. class CangKu  
  19. {  
  20.     public int num=0;  
  21.     boolean isEpt=true;  
  22.     static Object l=new Object();  
  23.       
  24.     public CangKu(int n)  
  25.     {  
  26.         this.num=n;  
  27.     }  
  28.       
  29.     public synchronized void put(int i)  
  30.     {  
  31.         //System.out.println(Thread.currentThread()+" put"+i);  
  32.         //synchronized(l)  
  33.         {  
  34.             if(!isEpt)  
  35.             {  
  36.                 try {  
  37.                     this.wait();  
  38.                 } catch (InterruptedException e) {  
  39.                     // TODO Auto-generated catch block  
  40.                     e.printStackTrace();  
  41.                 }  
  42.             }     
  43.           
  44.             num=i;  
  45.             isEpt=false;  
  46.             System.out.println(Thread.currentThread()+"=>put"+i);  
  47.               
  48.             notifyAll();  
  49.         }  
  50.     }  
  51.       
  52.     public synchronized int get()  
  53.     {  
  54.         //System.out.println(Thread.currentThread()+" get");  
  55.         //synchronized(l)  
  56.         {  
  57.             if(isEpt )  
  58.             {  
  59.                 try {  
  60.                     this.wait();  
  61.                 } catch (InterruptedException e) {  
  62.                     // TODO Auto-generated catch block  
  63.                     e.printStackTrace();  
  64.                 }  
  65.                   
  66.                   
  67.             }  
  68.   
  69.             isEpt=true;  
  70.             System.out.println("         "+Thread.currentThread()+"=>get"+num);  
  71.               
  72.             notifyAll();  
  73.             return num;  
  74.               
  75.               
  76.         }  
  77.     }  
  78.       
  79.       
  80. }  
  81.   
  82.   
  83. class P implements Runnable{  
  84.       

抱歉!评论已关闭.