1.冒泡
- static void sort(int[] arry)
- {
- for(int i=0;i<arry.length-1;i++)
- {
- //System.out.println("lentgh=>"+arry.length);
- if(arry[i]<arry[i+1])
- {
- int temp=0;
- //System.out.println("[i]=>"+arry[i]);
- //System.out.println("[i]+1=>"+arry[i+1]);
- temp=arry[i];
- arry[i]=arry[i+1];
- arry[i+1]=temp;
- i=-1;//从新检查排序, 自加后归零
- }
- }
- }
2.按规定的个数,给字符串截取成若干组
- String test="123156498478645614651";
- for(int i=0;i<test.length();i+=5)
- {
- System.out.println(test.substring(i, ( i+5 >test.length() ? test.length() :i+5 )));
- }
3.生产者消费者
- public class Per_Cer {
- public static void main(String[] args) {
- CangKu ck=new CangKu(5);
- P p =new P(ck);
- C c =new C(ck);
- Thread t1=new Thread(p);
- Thread t2=new Thread(c);
- t1.start();
- t2.start();
- }
- }
- class CangKu
- {
- public int num=0;
- boolean isEpt=true;
- static Object l=new Object();
- public CangKu(int n)
- {
- this.num=n;
- }
- public synchronized void put(int i)
- {
- //System.out.println(Thread.currentThread()+" put"+i);
- //synchronized(l)
- {
- if(!isEpt)
- {
- try {
- this.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- num=i;
- isEpt=false;
- System.out.println(Thread.currentThread()+"=>put"+i);
- notifyAll();
- }
- }
- public synchronized int get()
- {
- //System.out.println(Thread.currentThread()+" get");
- //synchronized(l)
- {
- if(isEpt )
- {
- try {
- this.wait();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- isEpt=true;
- System.out.println(" "+Thread.currentThread()+"=>get"+num);
- notifyAll();
- return num;
- }
- }
- }
- class P implements Runnable{