Java复习回顾---7月10日回顾练习

arithmetic    算法

 

String类:

 1 package cn.ftf.JavaAPI;
 2 /**
 3  * String类位于java.lang中,不需要导包就可使用
 4  * @author user
 5  *
 6  */
 7 public class TestString {
 8     //String类的初始化
 9     public static void main(String[] args) {
10         String a=new String();    //新建一个空的字符串
11         String b=new String("hello word!");    //新建一个内容为hello word的字符串
12         char [] character=new char[] {'a','b','c'};
13         String c=new String(character);    //新建一个内容为字符数组的字符串
14         System.out.println(a+b+c);
15         
16         //字符串的基本操作
17         String d=" hello word!!!";  //声明字符串
18         System.out.println(d.length());  //字符串的长度
19         System.out.println(d.charAt(2));  //某个位置的字符串
20         System.out.println(d.indexOf("o"));  //某个字符第一次出现的位置
21         System.out.println(d.lastIndexOf("o"));   ////某个字符最后一次出现的位置
22         System.out.println(d.indexOf("!!"));  ////某个子字符串第一次出现的位置
23     
24     
25     //字符串的转换操作
26         char []e=d.toCharArray();    //将字符串转换为字符数组
27         System.out.println(d.toUpperCase());    //将字符串转换为大写字母
28         System.out.println(String.valueOf(32));  //将int型数字转换为字符串
29         
30     //字符串的替代和去除空格操作
31         System.out.println("==============");
32         System.out.println(d.replace('e', 'E'));  //替换字符串中的字符或子字符串
33         System.out.println(d.replace("ll", "LL"));
34         System.out.println(d.trim());    //去除字符串两端的空格
35         
36         
37     //字符串的判断操作
38         System.out.println(d.startsWith(" h"));  //判断字符串是否为某一字符或字符串开头
39         System.out.println(d.endsWith("!"));    // //判断字符串是否为某一字符或字符串结尾
40         System.out.println(d.contains("llo"));    //判断是否包含某一字符串
41         System.out.println(d.isEmpty());   //判断是否为空 
42         System.out.println(d.equals(a));  //判断两个字符串是否相等  ==判断地址是否相同即是否为同一个对象,equals判断内容是否相同
43         
44         
45     //字符串的截取和分割
46         
47         
48        String str ="房廷飞-加油-鸭";
49        //截取操作
50        System.out.println(str.substring(3));    //从第四个字符截取到最后的字符串
51        System.out.println(str.substring(3, 5));  //截取第三个到第六个的字符串
52        
53        //字符串分割
54        String [] strArray=str.split("-");    //将字符串转换为字符串数组,split方法
55        for(int i=0;i<strArray.length;i++) {
56            System.out.println(strArray[i]+"\n");
57        }
58        
59     }
60 
61 }
62 
63 /**
64  * 运行结果:
65  * 
66  * hello word!abc
67 14
68 e
69 5
70 8
71 11
72  HELLO WORD!!!
73 32
74 ==============
75  hEllo word!!!
76  heLLo word!!!
77 hello word!!!
78 true
79 true
80 true
81 false
82 false
83 -加油-鸭
84 -加
85 房廷飞
86 
87 加油·
88 
89 90 */

StringBuffer类:

 1 package cn.ftf.JavaAPI;
 2 /**
 3  * StringBuffer类像是容器,内容和长度都可以修改
 4  * StringBuffer类没有equal方法
 5  * StringBuffer类中的字符串不能相加
 6  * 
 7  * @author 房廷飞
 8  *
 9  */
10 public class TestStringBuffer {
11     public static void main(String[] args) {
12         StringBuffer sb=new StringBuffer("hello word!");    //StringBuffer类的定义,只能这样定义
13         sb.setCharAt(3, 'x');  //修改指定位置字符
14         System.out.println(sb);  
15         
16         sb.replace(1, 3, "qq");  //替换指定位置字符
17         System.out.println(sb);
18         
19         sb.append("hahahaha");  //在结尾插入字符串
20         System.out.println(sb);
21         
22         sb.insert(2, "fang");  //在指定位置插入字符串
23         System.out.println(sb);
24         
25         sb.delete(2, 7);  //指定范围删除
26         System.out.println(sb);
27         
28         sb.deleteCharAt(2);  //指定位置删除
29         System.out.println(sb);
30         
31         sb.delete(0, sb.length());  //清空缓存区
32         System.out.println("空"+sb);
33         
34     }
35 
36 }
37 
38 /**
39 输出结果:
40 helxo word!
41 hqqxo word!
42 hqqxo word!hahahaha
43 hqfangqxo word!hahahaha
44 hqxo word!hahahaha
45 hqo word!hahahaha
46 47 */

一个小程序,计数字符串中某个字符出现的次数:

 1 package cn.ftf.JavaAPI;
 2 
 3 public class FindTime {
 4     public static void main(String[] args) {
 5         String str="hello word hello word!!!";
 6         String key="!";
 7         int count=find(str,key);
 8         System.out.println(count);
 9         
10     }
11     public static int find(String str,String key) {
12         int count=0;
13         if(!str.contains(key)) {
14             return 0;
15         }else {
16             while(str.contains(key)) {
17             int time=str.indexOf(key);
18             str=str.substring(time+1);
19             count++;
20             }
21             return count;
22         }    
23     }
24     }

一个小程序,给字符串中的数字排序(字符串分割,toArrary ,再用 StringBuffer 类 toString):

 1 package cn.ftf.JavaAPI;
 2 
 3 import java.util.Arrays;
 4 
 5 public class StrToArray {
 6     public static void main(String[] args) {
 7         String str="1 5 23 78 99 33 54 67 we -22";
 8         String []str1=str.split(" ");  //分割字符串。并将分割的部分组成数组
 9         Arrays.sort(str1);    //对数组自动排序,虽然很智障
10         StringBuffer str2=new StringBuffer();    //通过StringBuffer类将数组中的元素一个一个写入字符串中
11         for(int i=0;i<str1.length;i++) {
12             str2.append(str1[i]+" ");
13         }
14         System.out.println(str2);
15         
16         
17     }
18 
19 }

 System类简单了解了一下:

 1 package cn.ftf.JavaAPI;
 2 
 3 import java.util.Properties;
 4 
 5 public class TestSystem {
 6     public static void main(String[] args) {
 7         Properties properties=System.getProperties();    //获取系统当前属性
 8         System.out.println(properties);
 9         long startTime=System.currentTimeMillis();    //获取当前时间戳
10         int a=0;
11         for(int i=0;i<100;i++) {
12             a++;
13         }
14         long endTime=System.currentTimeMillis();
15         System.out.println(endTime+a);
16         System.out.println(endTime-startTime+"hello");
17 
18     }
19 
20 }

Runtime类简单了解了一下:

 1 package cn.ftf.JavaAPI;
 2 
 3 import java.io.IOException;
 4 
 5 public class TestRuntime {
 6     public static void main(String[] args) throws IOException, InterruptedException {
 7         Runtime re=Runtime.getRuntime();  //获取实例
 8         System.out.println(re.availableProcessors());    //获得处理器个数
 9         System.out.println(re.freeMemory()/1020/1024+"M");    //空闲内存空间
10         System.out.println(re.maxMemory()/1024/1024+"M");    //最大内存空间
11         
12         //re.exec("notepad.exe");    //打开记事程序,相当于dos功能
13         
14         Process prosess=re.exec("notepad.exe");  ////或实例化对象,打开记事本程序
15         Thread.sleep(3000);    //设置延迟
16         prosess.destroy();    //关掉进程
17     }
18 
19 }

Random类,生成随机数就靠它

 1 package cn.ftf.JavaAPI;
 2 
 3 import java.util.Random;
 4 
 5 public class TestRandom {
 6     public static void main(String[] args) {
 7         Random ra=new Random();
 8         System.out.println(ra.nextInt(100));    //随机生成0——100内的整数
 9         System.out.println(ra.nextDouble());    //随机生成0——1之间的双精度浮点数
10     }
11 
12 }

包装类,装箱和拆箱:

 1 package cn.ftf.JavaAPI;
 2 
 3 public class TestWrapClass {
 4     public static void main(String[] args) {
 5         int a=12;
 6         Integer in=new Integer(a);    //装箱
 7         int b=in.intValue();    //拆箱
 8         
 9         int c=Integer.parseInt("34");    //将字符串转换为int型
10         Integer d=Integer.valueOf("56");    //将字符串转化为包装类
11         
12         //自动装箱和自动拆箱
13         int e=12;
14         Integer f=e;    //自动装箱
15         Integer g=12;
16         
17         int h=f;    //自动拆箱
18         
19         int i=add(f,g);
20         System.out.println(i);
21         
22     }
23     public static int add(Integer a,Integer b) {    //JDK5.0以后基本类型和包装类型可以混合数学运算
24         return a+b;
25     }
26 
27 }

Come on!  明天集合类,加油鸭!!!

猜你喜欢

转载自www.cnblogs.com/fangtingfei/p/11165713.html