《day18_String练习_基本类型包装类_集合入门》

 1 package cn.itcast.api.String.test;
 2 
 3 public class StringTest_1 {
 4 
 5     public static void main(String[] args) {
 6 
 7         String s1 = "asdfitcastghijfghjk";
 8         String s2 = "xcitcastvbnm";
 9 
10         String maxSubStirng = getMaxSubstring(s2, s1);
11         System.out.println("maxSubString:" + maxSubStirng);
12         /*
13          * 作业1:获取两个字符串的最大相同子串。 "asdfitcastghijfghjk" "xcitcastvbnm"
14          * 
15          * 思路: 1,先明确两个字符串的长短,在长串中判断短串是否存在。 2,存在,已找到,说明短串就是最大的相同。
16          * 不存在,就将短串按照长度递减的方式,获取短串中的子串,并到长串中判断。 3,一旦存在,便结束查找。
17          */
18 
19     }
20 
21     private static String getMaxSubstring(String s1, String s2) {
22         String max, min;
23         // 明确哪个是长串,哪个是短串。
24         max = (s1.length() > s2.length()) ? s1 : s2;
25         min = max.equals(s1) ? s2 : s1;
26 
27         // 验证max和min.
28         /*
29          * System.out.println("max:"+max); System.out.println("min:"+min);
30          */
31 
32         for (int i = 0; i < min.length(); i++) {
33             for (int start = 0, end = min.length() - i; end < min.length(); start++, end++) {
34                     String temp = min.substring(start,end);
35                     if(max.contains(temp)){
36                         return temp;
37                     }
38             }
39         }
40         return null;
41     }
42 
43 }
 1 package cn.itcast.api.String.test;
 2 
 3 import java.util.Arrays;
 4 
 5 public class StringTest_2 {
 6 
 7     public static void main(String[] args) {
 8         String str = "cfdasbv";
 9         str = sortStringByChars(str);
10         System.out.println("str="+str);
11 
12     }
13     /*
14      * 
15      * 作业2:对字符串中字符进行自然顺序排序。
16      * cfdasbv--->abcdfsv
17      * 
18      * 思路:
19      * 1,排序都是对数组排序。
20      * 2,数组中的元素都是在字符串中,把字符串转成数组。
21      * 3,对数组排序。
22      * 4,将排序后的数组转成字符串。
23      */
24 
25     public static String sortStringByChars(String str) {
26         //1,将字符串转成数组。转成字符数组。
27         char[] chs = getArray(str);
28         //2,对数组排序。
29         sort(chs);
30         //3,将排序后的数组转成字符串。返回。
31         return new String(chs);
32     }
33 
34     private static void sort(char[] chs) {
35         Arrays.sort(chs);
36         
37     }
38 
39     /**
40      * 将字符串转成字符数组。
41      * @param str
42      * @return
43      */
44     private static char[] getArray(String str) {
45         // TODO Auto-generated method stub
46         return str.toCharArray();
47     }
48 
49 }
 1 package cn.itcast.api.String.test;
 2 
 3 public class StringTest_3 {
 4     public static void main(String[] args){
 5         String str = "   it  cast    ";
 6 //        String s1 = str.trim();
 7         String s1 = myTrim(str);
 8         System.out.println("-"+s1+"-");
 9     }
10 
11     /**
12      * 模拟trim功能。
13      * 
14      */
15     public static String myTrim(String str){
16             
17         //定义两个变量,一个记录头,一个记录尾。
18         int start = 0;
19         int end  =str.length()-1;
20         
21         //2,获取头部非空白的位置,
22         while(start<=end && str.charAt(start)==' '){
23             start++;
24         }
25         //2,获取尾部非空白的位置,
26         while(start<=end && str.charAt(end)==' '){
27             end--;
28         }
29         
30         //截取字符串。
31         return str.substring(start, end+1);
32     }
33 }
 1 package cn.itcast.api.wrapper.demo;
 2 
 3 public class WrapperDemo {
 4 
 5     public static void main(String[] args) {
 6         /*
 7          * 场景:通过文本框获取用户输入的数字数据,可是得到的都是字符串。
 8          * 如果想要对字符串中的数字进行运算,必须要将字符串转成数字。
 9          * JAVA提供了相应的解决对象。
10          * 基本数据类型对象包装类:Java将基本数据类型值封装成对象。
11          * 封装成对象有什么好处?因为可以提供更多的操作基本数值的方法。
12          * 
13          * byte     Byte
14          * short    Short
15          * int         Integer
16          * long        Long
17          * float    Float
18          * double    Double
19          * boolean    Boolean
20          * char        Character
21          */
22         
23         //学习一下整数Integer
24         /*
25          * 基本类型对象包装类特点:
26          * 1,用于在基本数据类型和字符串之间进行转换。
27          * int parseInt(String);
28          * byte parseByte(String);
29          * boolean parseBoolean(String);
30          * 
31          * xxx parseXxx(String);
32          * 只有Character没哟解析方法。
33          * 
34          * 
35          */
36         
37         /*System.out.println(Integer.MAX_VALUE);
38         System.out.println(Integer.toBinaryString(10));
39         System.out.println(Integer.toBinaryString(-6));*/
40         
41         //1,字符串--->基本数值。结果:基本数值(字符串)演示Integer  int(String)
42         
43         System.out.println(Integer.parseInt("123")+2);//NumberFormaExctpioon
44         System.out.println(Integer.parseInt("2222", 10));//按照指定的进制进行转换。
45         
46         //2,基本数值转成字符串。34+""  String.valueOf(34)Integer.toString(int);
47         System.out.println(34+5);
48         
49         //3,基本数值---》包装对象。
50         Integer i = new Integer(4);
51         Integer ii = Integer.valueOf("4");
52         
53         //包装对象---》基本数据类型
54         int num = i.intValue();
55         System.out.println(num);
56     }
57 
58 }
 1 package cn.itcast.api.wrapper.demo;
 2 
 3 public class WrapperDemo2 {
 4 
 5     public static void main(String[] args) {
 6         
 7 //        int i = 4;
 8 //        Integer i = new Integer(4);
 9 //        JDK1.5以后,有了一个包装类的新特性。目的简化书写。自动装箱
10         Integer i = 4;//自动装箱。Integer.valueOf(4);
11         i = i+5;//原理是:等号右边将i对象转成基本数值。i.intValue()+5;加法运算后,在此装箱。
12         //i = Integer.valueOf(i.inValue()+5);
13         System.out.println(i);
14         
15         Integer a = new Integer(3);
16         Integer b = new Integer(3);
17         System.out.println(a==b);
18         System.out.println(a.equals(b));
19         
20         System.out.println("===============");
21         Integer x = 127;
22         Integer y = 127;
23         System.out.println(x==y);
24         System.out.println(x.equals(y));
25 
26     }
27 
28 }
 1 //WrapperTest.java
 2 package cn.itcast.api.wrapper.demo;
 3 
 4 import java.util.Arrays;
 5 
 6 public class WrapperTest {
 7 
 8     public static void main(String[] args) {
 9         /*
10          * 练习:面试题:
11          * "23 9 -4 18 100 7";
12          * 要求对这串数字按照从大到小排序,生成一个数值有序的字符串。
13          */
14         String numString = "23 9 -4 18 100 7";
15         numString = sortNumberString(numString);
16         System.out.println("nums="+numString);
17 
18     }
19 
20     public static String sortNumberString(String numString) {
21         //1一个字符串通过分割变成多个字符串。split();
22         String[] strs = numString.split(" ");
23         
24         //2,不能直接对字符串大小排序。因为字符串23比字符串9要小。必须转成整数值。
25         //将字符串数组转成int[] 数组。
26         int[] nums = parseIntArray(strs);
27         
28         //3,对数组排序。
29         Arrays.sort(nums);
30         
31 //        4,将数组转成字符串。
32         return toString(nums);
33     }
34     private static String toString(int[] nums) {
35         StringBuilder sb  = new StringBuilder();
36         for (int i = 0; i < nums.length; i++) {
37             if(i!=nums.length-1){
38                 sb.append(nums[i]+" ");
39             }else{
40                 sb.append(nums[i]);
41             }
42         }
43         return sb.toString();
44     }
45 
46     //将字符串数组转成int[] 数组
47     public static int[] parseIntArray(String[] strs) {
48         //定义一个int数组。
49         int[] arr = new int[strs.length];
50         
51         //2,遍历字符串数组。把元素转成int存储到int数组中。
52         for (int i = 0; i < strs.length; i++) {
53             arr[i] = Integer.parseInt(strs[i]);
54         }
55         return arr;
56     }
57 
58 }
原文地址:https://www.cnblogs.com/sun-/p/5424394.html