1.冒泡
- staticvoidsort(int[]arry)
- {
- for(inti=0;i<arry.length-1;i++)
- {
- //System.out.println("lentgh=>"+arry.length);
- if(arry[i]<arry[i+1])
- {
- inttemp=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.按规定的个数,给字符串截取成若干组
- Stringtest="123156498478645614651";
- for(inti=0;i<test.length();i+=5)
- {
- System.out.println(test.substring(i,(i+5>test.length()?test.length():i+5)));
- }
3.生产者消费者
- publicclassPer_Cer{
- publicstaticvoidmain(String[]args){
- CangKuck=newCangKu(5);
- Pp=newP(ck);
- Cc=newC(ck);
- Threadt1=newThread(p);
- Threadt2=newThread(c);
- t1.start();
- t2.start();
- }
- }
- classCangKu
- {
- publicintnum=0;
- booleanisEpt=true;
- staticObjectl=newObject();
- publicCangKu(intn)
- {
- this.num=n;
- }
- publicsynchronizedvoidput(inti)
- {
- //System.out.println(Thread.currentThread()+"put"+i);
- //synchronized(l)
- {
- if(!isEpt)
- {
- try{
- this.wait();
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- num=i;
- isEpt=false;
- System.out.println(Thread.currentThread()+"=>put"+i);
- notifyAll();
- }
- }
- publicsynchronizedintget()
- {
- //System.out.println(Thread.currentThread()+"get");
- //synchronized(l)
- {
- if(isEpt)
- {
- try{
- this.wait();
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- isEpt=true;
- System.out.println(""+Thread.currentThread()+"=>get"+num);
- notifyAll();
- returnnum;
- }
- }
- }
- classPimplementsRunnable{
- publicCangKuck=null;
- publicP(CangKuc)
- {
- this.ck=c;
- }
- @Override
- publicvoidrun(){
- while(true)
- {//
- //System.out.println("pppppppppp");
- ck.put((int)(Math.random()*100));
- try{
- Thread.sleep((int)(Math.random()*2000));
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- }
- }
- classCimplementsRunnable{
- publicCangKuck=null;
- publicC(CangKuc)
- {
- this.ck=c;
- }
- @Override
- publicvoidrun(){
- while(true)
- {
- //System.out.println("cccccccccccccc"+);
- ck.get();
- try{
- Thread.sleep((int)(Math.random()*2000));
- }catch(InterruptedExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- }
- }
- }
1:用单层for循环控制输出乘法表
//单层for, 当行列数相等时换行
for(int i=1,j=1; j<=9;i++)
{
System.out.print(" "+i*j+" ");
if(i == j) //行列号相等时, 就该换行了
{
i=0;
j++;
System.out.println();
}
}
2.素数中用"开方" 缩小循环范围
public static boolean isP(int i)
{
if(i==0 || i==1 )//一:2是素数,不用剔除出去
{
return false;
}
double iii = Math.sqrt(i);//二:取平方根Math.sqrt(i), 且应该是<= 比如i为4
for(int j=2;j<=iii;j++) //三:写在for里边影响效率;j<=(Math.sqrt(i)); 写在外边更好
{
if(i%j==0)
{
return false;
}
}
return true;
}
3.回文数 ( ABCCBA)
public static boolean isH(int n)
{
if( n == revsNum(n) ) //原参数 是否等于反转后的参数
{
return true;
}
else
{
return false;
}
}
public static int revsNum(int n)
{
int old = n;
int tar = 0;
while(old > 0) //这个比较有意思, 用整除10的方式筛选出最低位数字
{
tar *= 10; //相当于把上次筛选出来的数字左移
tar += old % 10;//加上新筛选出的最低位
old /= 10;//原数字可以降低位数了
}
//System.out.println("tar=>"+tar);
return tar;
}
4.Date对象的操作
Date dd =new Date(d.getTime() + 1000*60*60*24); //getTime返回计算机时间
5.
要求:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
1./**
2. * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
3. * 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,
4. * 应该输出为"我ABC"而不是"我ABC+汉的半个"。
5. */
6. public static String subString(String str,int len){
7. if(str == null && "".equals(str)){
8. return null;
9. }
10. //将字符串中的char数组转换成指定编码方式的byte数组的函数
11. byte[] strBytes = null;
12. try {
13. strBytes = str.getBytes("GBK");
14.
15. } catch (UnsupportedEncodingException e) {
16. e.printStackTrace();
17. }
18. //得到字符串的长度,判断截取字符串的长度是否在判断的范围内,否则返回原串
19. int strLen = strBytes.length;
20. if(len >= strLen || len < 1){
21. return str;
22. }
23.// System.out.println("strBytes.length="+strBytes.length);
24.// System.out.println("len="+len);
25. int count = 0;
26. for(int i=0; i<len; i++){
27. //将每个字节数组转换为整型数,以为后面根据值的正负来判断是否为汉字
28. int value = strBytes[i];
29.// System.out.print(value+",");
30. //如果是汉字(负),则统计截取字符串中的汉字所占字节数
31. if(value < 0){
32. count++;
33. }
34.// System.out.println("zh count="+count);
35. }
36. //依据判断给定的字符串是否含有汉字,利用String类的substring()方法来截取不同的长度
37.
38. //根据所统计的字节数,判断截取到字符是否为半个汉字,奇数为半个汉字
39. if(count % 2 !=0){
40. //如果在截取长度为1时,则将该汉字取出,
41. //其他情况则不截取这里的截取长度则按字符长度截取(截取字节长度数-截取汉字字节数/2-截取到的半个汉字的字节数)
42. len = (len == 1)?len:len-count/2-1;
43.// System.out.println("处理后的len="+len);
44.
45. }else{
46. //截取字符长度为字节长度-汉字所占字节长度/2(汉字占两个字节)
47. len = len-(count/2);
48. }
49. return str.substring(0,len);
50.
51. }
52. public static void main(String[] args) {
53. //情况一:
54. String inStr = "我ABC你";
55. String str = subString(inStr, 6);
56. System.out.println(str); //我ABC
57.
58. //情况二:首字符为汉字
59. inStr = "我ABC汉DEF";
60. str = subString(inStr, 1);
61. System.out.println(str); //我
62.
63. //情况三:中间有连续汉字
64. inStr = "我AB爱孩子CDEF";
65. str = subString(inStr,9);
66. System.out.println(str); //我AB爱孩
67.
68. //情况四:没有汉字
69. inStr = "ABCDEF";
70. str = subString(inStr,4);
71. System.out.println(str); //ABCD
72. }