13. 常见类 (StringBuffer、Array、Integer、Character)

来源:互联网 发布:vr室内设计软件 编辑:程序博客网 时间:2024/06/06 12:44

1: 字符串缓冲区类----StringBuffer(掌握)

       (1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了一个字符串缓冲区类StringBuffer供我们使用。

 * 线程安全(多线程讲解)

 * 安全 -- 同步 -- 数据是安全的

 * 不安全 -- 不同步 -- 效率高一些

 * 安全和效率问题是永远困扰我们的问题。

 * 安全:医院的网站,银行网站

 * 效率:新闻网站,论坛之类的

 * StringBuffer:     线程安全的可变字符串。

       (2)StringBuffer的构造方法

              public StringBuffer():                         无参构造方法

             publicStringBuffer(int capacity):         指定容量的字符串缓冲区对象

              public StringBuffer(String str):            指定字符串内容的字符串缓冲区对象

        StringBuffer的方法:

 *           publicint capacity():                        返回当前容量。    理论值

 *           publicint length():                              返回长度(字符数)。 实际值

public class StringBufferDemo {       publicstatic void main(String[] args) {              //public StringBuffer():无参构造方法              StringBuffersb = new StringBuffer();              System.out.println("sb:"+ sb);              System.out.println("sb.capacity():"+ sb.capacity());              System.out.println("sb.length():"+ sb.length());              System.out.println("--------------------------");              //public StringBuffer(int capacity):指定容量的字符串缓冲区对象              StringBuffersb2 = new StringBuffer(50);              System.out.println("sb2:"+ sb2);              System.out.println("sb2.capacity():"+ sb2.capacity());              System.out.println("sb2.length():"+ sb2.length());              System.out.println("--------------------------");              //public StringBuffer(String str):指定字符串内容的字符串缓冲区对象              StringBuffersb3 = new StringBuffer("hello");              System.out.println("sb3:"+ sb3);              System.out.println("sb3.capacity():"+ sb3.capacity());              System.out.println("sb3.length():"+ sb3.length());       }}

       (3)StringBuffer的常见功能

       A:添加功能

public StringBuffer append(String str):

可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身

public StringBuffer insert(int offset,String str):

在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

public class StringBufferDemo {       publicstatic void main(String[] args) {              //创建字符串缓冲区对象              StringBuffersb = new StringBuffer();              //public StringBuffer append(String str)              //StringBuffer sb2 = sb.append("hello");              //System.out.println("sb:" + sb);              //System.out.println("sb2:" + sb2);              //System.out.println(sb == sb2); // true              //一步一步的添加数据              //sb.append("hello");              //sb.append(true);              //sb.append(12);              //sb.append(34.56);              //链式编程              sb.append("hello").append(true).append(12).append(34.56);              System.out.println("sb:"+ sb);              //public StringBuffer insert(int offset,String// str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身              sb.insert(5,"world");              System.out.println("sb:"+ sb);       }}

       B:删除功能

public StringBuffer deleteCharAt(int index): 删除指定位置的字符,并返回本身

public StringBuffer delete(int start,int end):  删除从指定位置开始到指定位置结束的内容,并返回本身

public class StringBufferDemo {       publicstatic void main(String[] args) {              //创建对象              StringBuffersb = new StringBuffer();               //添加功能              sb.append("hello").append("world").append("java");              System.out.println("sb:"+ sb);              //public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身              //需求:我要删除e这个字符,肿么办?              //sb.deleteCharAt(1);              //需求:我要删除第一个l这个字符,肿么办?              //sb.deleteCharAt(1);              //public StringBuffer delete(int start,int              //end):删除从指定位置开始指定位置结束的内容,并返回本身              //需求:我要删除world这个字符串,肿么办?              //sb.delete(5, 10);              //需求:我要删除所有的数据              sb.delete(0,sb.length());              System.out.println("sb:"+ sb);       }}

       C:替换功能

public StringBuffer replace(int start,int end,String str):从start开始到end用str替换

public class StringBufferDemo {   publicstatic void main(String[] args) {          //创建字符串缓冲区对象          StringBuffersb = new StringBuffer();          //添加数据          sb.append("hello");          sb.append("world");          sb.append("java");          System.out.println("sb:"+ sb);          //public StringBuffer replace(int start,int end,String          //str):从start开始到end用str替换          //需求:我要把world这个数据替换为"节日快乐"          sb.replace(5,10, "节日快乐");          System.out.println("sb:"+ sb);   }}

       D:反转功能

public StringBuffer reverse()

public class StringBufferDemo {       publicstatic void main(String[] args) {              //创建字符串缓冲区对象              StringBuffersb = new StringBuffer();              //添加数据              sb.append("霞青林爱我");              System.out.println("sb:"+ sb);              //public StringBuffer reverse()              sb.reverse();              System.out.println("sb:"+ sb);       }}

       E:截取功能    (注意返回值类型不再是StringBuffer本身了)

public String substring(int start)

public String substring(int start,int end)

public class StringBufferDemo {   publicstatic void main(String[] args) {          //创建字符串缓冲区对象          StringBuffersb = new StringBuffer();          //添加元素          sb.append("hello").append("world").append("java");          System.out.println("sb:"+ sb);          //截取功能          //public String substring(int start)          Strings = sb.substring(5);          System.out.println("s:"+ s);          System.out.println("sb:"+ sb);          //public String substring(int start,int end)          Stringss = sb.substring(5, 10);          System.out.println("ss:"+ ss);          System.out.println("sb:"+ sb);   }}

(4)StringBufferString的区别?

 * 前者长度和内容可变,后者不可变。

 * 如果使用前者做字符串的拼接,不会浪费太多的资源。

       (5)StringBuffer的练习(做一遍)

              A:StringStringBuffer相互转换

                     String-- StringBuffer

                            构造方法

                                   StringBuffer -- String

                                   toString()方法

public class StringBufferTest {   publicstatic void main(String[] args) {          //String -- StringBuffer          Strings = "hello";          //注意:不能把字符串的值直接赋值给StringBuffer          //StringBuffer sb = "hello";          //StringBuffer sb = s;          //方式1:通过构造方法          StringBuffersb = new StringBuffer(s);          //方式2:通过append()方法          StringBuffersb2 = new StringBuffer();          sb2.append(s);          System.out.println("sb:"+ sb);          System.out.println("sb2:"+ sb2);          System.out.println("---------------");          //StringBuffer -- String          StringBufferbuffer = new StringBuffer("java");          //String(StringBuffer buffer)          //方式1:通过构造方法          Stringstr = new String(buffer);          //方式2:通过toString()方法          Stringstr2 = buffer.toString();          System.out.println("str:"+ str);          System.out.println("str2:"+ str2);   }}

              B:把数组拼接成一个字符串

public class StringBufferTest2 {       publicstatic void main(String[] args) {              //定义一个数组              int[]arr = { 44, 33, 55, 11, 22 };              //定义功能              //方式1:用String做拼接的方式              Strings1 = arrayToString(arr);              System.out.println("s1:"+ s1);              //方式2:用StringBuffer做拼接的方式              Strings2 = arrayToString2(arr);              System.out.println("s2:"+ s2);       }       //用StringBuffer做拼接的方式       publicstatic String arrayToString2(int[] arr) {              StringBuffersb = new StringBuffer();               sb.append("[");              for(int x = 0; x < arr.length; x++) {                     if(x == arr.length - 1) {                            sb.append(arr[x]);                     }else {                            sb.append(arr[x]).append(",");                     }              }              sb.append("]");              returnsb.toString();       }       //用String做拼接的方式       publicstatic String arrayToString(int[] arr) {              Strings = "";              s+= "[";              for(int x = 0; x < arr.length; x++) {                     if(x == arr.length - 1) {                            s+= arr[x];                     }else {                            s+= arr[x];                            s+= ", ";                     }              }              s+= "]";              return  s;       }}

              C:把字符串反转

public class StringBufferTest3 {   publicstatic void main(String[] args) {          //键盘录入数据          Scannersc = new Scanner(System.in);          System.out.println("请输入数据:");          Strings = sc.nextLine();          //方式1:用String做拼接          Strings1 = myReverse(s);          System.out.println("s1:"+ s1);          //方式2:用StringBuffer的reverse()功能          Strings2 = myReverse2(s);          System.out.println("s2:"+ s2);   }   //用StringBuffer的reverse()功能   publicstatic String myReverse2(String s) {          //StringBuffer sb = new StringBuffer();          //sb.append(s);          //StringBuffer sb = new StringBuffer(s);          //sb.reverse();          //return sb.toString();          //简易版          returnnew StringBuffer(s).reverse().toString();   }   //用String做拼接   publicstatic String myReverse(String s) {          Stringresult = "";          char[]chs = s.toCharArray();          for(int x = chs.length - 1; x >= 0; x--) {                 //char ch = chs[x];                 //result += ch;                 result+= chs[x];          }          return  result;   }}

              D:判断一个字符串是否对称

 * 例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串

 * 分析:

 *          判断一个字符串是否是对称的字符串,我只需要把

 *                 第一个和最后一个比较

 *                 第二个和倒数第二个比较

 *                 ...

 *          比较的次数是长度除以2。

public class StringBufferTest4 {       publicstatic void main(String[] args) {              //创建键盘录入对象              Scannersc = new Scanner(System.in);              System.out.println("请输入一个字符串:");              Strings = sc.nextLine();              //一个一个的比较              booleanb = isSame(s);              System.out.println("b:"+ b);              //用字符串缓冲区的反转功能              booleanb2 = isSame2(s);              System.out.println("b2:"+b2);       }       publicstatic boolean isSame2(String s) {              returnnew StringBuffer(s).reverse().toString().equals(s);       }       //public static boolean isSame(String s) {       //// 把字符串转成字符数组       //char[] chs = s.toCharArray();       //for (int start = 0, end = chs.length - 1; start <= end; start++, end--) {       //if (chs[start] != chs[end]) {       //return false;       //}       //}       //return true;       //}       publicstatic boolean isSame(String s) {              booleanflag = true;              //把字符串转成字符数组              char[]chs = s.toCharArray();              for(int start = 0, end = chs.length - 1; start <= end; start++, end--) {                     if(chs[start] != chs[end]) {                            flag =false;                            break;                     }              }              return  flag;       }}

       (6)面试题

              小细节

                     StringBuffer同步的数据安全效率低。

                     StringBuilder不同步的数据不安全效率高。

             

A:String, StringBuffer,   StringBuilder的区别

String是内容不可变的StringBuffer,StringBuilder都是内容可变的。

StringBuffer是同步的数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高

       B:StringBuffer和数组的区别?

二者都可以看出是一个容器,装其他的数据。

但是呢,StringBuffer的数据最终是一个字符串数据。

而数组可以放置多种数据,但必须是同一种数据类型的。

       (7)注意的问题:

              String作为形式参数,StringBuffer作为形式参数。

形式参数:

            基本类型:形式参数的改变不影响实际参数

            引用类型:形式参数的改变直接影响实际参数

注意:String作为参数传递,效果和基本类型作为参数传递是一样的

public class StringBufferDemo {       publicstatic void main(String[] args) {              Strings1 = "hello";              Strings2 = "world";              System.out.println(s1+ "---" + s2);// hello---world              change(s1,s2);              System.out.println(s1+ "---" + s2);// hello---world              StringBuffersb1 = new StringBuffer("hello");              StringBuffersb2 = new StringBuffer("world");              System.out.println(sb1+ "---" + sb2);// hello---world              change(sb1,sb2);              System.out.println(sb1+ "---" + sb2);// hello---worldworld       }       publicstatic void change(StringBuffer sb1, StringBuffer sb2) {              sb1= sb2;              sb2.append(sb1);       }       publicstatic void change(String s1, String s2) {              s1= s2;              s2= s1 + s2;       }}

2:数组高级(掌握)

       (1)排序

           A:冒泡排序

相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处。同理,其他的元素就可以排好。

public class ArrayDemo {       publicstatic void main(String[] args) {              //定义一个数组              int[]arr = { 24, 69, 80, 57, 13 };              System.out.println("排序前:");              printArray(arr);              /*              //第一次比较              //arr.length - 1是为了防止数据越界              //arr.length - 1 - 0是为了减少比较的次数              for(int x = 0; x < arr.length - 1 - 0; x++) {                     if(arr[x] > arr[x + 1]) {                            inttemp = arr[x];                            arr[x]= arr[x + 1];                            arr[x+ 1] = temp;                     }              }              System.out.println("第一次比较后:");              printArray(arr);              //第二次比较              //arr.length - 1是为了防止数据越界              //arr.length - 1 - 1是为了减少比较的次数              for(int x = 0; x < arr.length - 1 - 1; x++) {                     if(arr[x] > arr[x + 1]) {                            inttemp = arr[x];                            arr[x]= arr[x + 1];                            arr[x+ 1] = temp;                     }              }              System.out.println("第二次比较后:");              printArray(arr);              //第三次比较              //arr.length - 1是为了防止数据越界              //arr.length - 1 - 2是为了减少比较的次数              for(int x = 0; x < arr.length - 1 - 2; x++) {                     if(arr[x] > arr[x + 1]) {                            inttemp = arr[x];                            arr[x]= arr[x + 1];                            arr[x+ 1] = temp;                     }              }              System.out.println("第三次比较后:");              printArray(arr);              //第四次比较              //arr.length - 1是为了防止数据越界              //arr.length - 1 - 3是为了减少比较的次数              for(int x = 0; x < arr.length - 1 - 3; x++) {                     if(arr[x] > arr[x + 1]) {                            inttemp = arr[x];                            arr[x]= arr[x + 1];                            arr[x+ 1] = temp;                     }              }              System.out.println("第四次比较后:");              printArray(arr);              */               //既然听懂了,那么上面的代码就是排序代码              //而上面的代码重复度太高了,所以用循环改进              //for (int y = 0; y < 4; y++) {              //for (int x = 0; x < arr.length - 1 - y; x++) {              //if (arr[x] > arr[x + 1]) {              //int temp = arr[x];              //arr[x] = arr[x + 1];              //arr[x + 1] = temp;              //}              //}              //}              /*              //由于我们知道比较的次数是数组长度-1次,所以改进最终版程序              for(int x = 0; x < arr.length - 1; x++) {                     for(int y = 0; y < arr.length - 1 - x; y++) {                            if(arr[y] > arr[y + 1]) {                                   inttemp = arr[y];                                   arr[y]= arr[y + 1];                                   arr[y+ 1] = temp;                            }                     }              }              System.out.println("排序后:");              printArray(arr);              */              //由于我可能有多个数组要排序,所以我要写成方法              bubbleSort(arr);              System.out.println("排序后:");              printArray(arr);       }       //冒泡排序代码       public static voidbubbleSort(int[] arr){              for (int x = 0; x< arr.length - 1; x++) {                     for (int y =0; y < arr.length - 1 - x; y++) {                            if(arr[y] > arr[y + 1]) {                                   inttemp = arr[y];                                   arr[y]= arr[y + 1];                                   arr[y+ 1] = temp;                            }                     }              }       }        //遍历功能       publicstatic void printArray(int[] arr) {              System.out.print("[");              for(int x = 0; x < arr.length; x++) {                     if(x == arr.length - 1) {                            System.out.print(arr[x]);                     }else {                            System.out.print(arr[x]+ ", ");                     }              }              System.out.println("]");       }}                  

           B:选择排序

                     把0索引的元素,和索引1以后的元素都进行比较,第一次完毕,最小值出现在了0索引。同理,其他的元素就可以排好。                    

public class ArrayDemo {   publicstatic void main(String[] args) {          //定义一个数组          int[]arr = { 24, 69, 80, 57, 13 };          System.out.println("排序前:");          printArray(arr);          /*          //第一次          intx = 0;          for(int y = x + 1; y < arr.length; y++) {                 if(arr[y] < arr[x]) {                        inttemp = arr[x];                        arr[x]= arr[y];                        arr[y]= temp;                 }          }          System.out.println("第一次比较后:");          printArray(arr);          //第二次          x= 1;          for(int y = x + 1; y < arr.length; y++) {                 if(arr[y] < arr[x]) {                        inttemp = arr[x];                        arr[x]= arr[y];                        arr[y]= temp;                 }          }          System.out.println("第二次比较后:");          printArray(arr);          //第三次          x= 2;          for(int y = x + 1; y < arr.length; y++) {                 if(arr[y] < arr[x]) {                        inttemp = arr[x];                        arr[x]= arr[y];                        arr[y]= temp;                 }          }          System.out.println("第三次比较后:");          printArray(arr);          //第四次          x= 3;          for(int y = x + 1; y < arr.length; y++) {                 if(arr[y] < arr[x]) {                        inttemp = arr[x];                        arr[x]= arr[y];                        arr[y]= temp;                 }          }          System.out.println("第四次比较后:");          printArray(arr);          */          /*          //通过观察发现代码的重复度太高,所以用循环改进          for(intx=0; x<arr.length-1; x++){                 for(inty=x+1; y<arr.length; y++){                        if(arr[y]<arr[x]){                               inttemp = arr[x];                               arr[x]= arr[y];                                arr[y] = temp;                        }                 }          }          System.out.println("排序后:");          printArray(arr);          */          //用方法改进          selectSort(arr);          System.out.println("排序后:");          printArray(arr);    }   publicstatic void selectSort(int[] arr){          for(intx=0; x<arr.length-1; x++){                 for(inty=x+1; y<arr.length; y++){                        if(arr[y]<arr[x]){                               inttemp = arr[x];                               arr[x]= arr[y];                                arr[y] = temp;                        }                 }          }   }   //遍历功能   publicstatic void printArray(int[] arr) {          System.out.print("[");          for(int x = 0; x < arr.length; x++) {                 if(x == arr.length - 1) {                        System.out.print(arr[x]);                 }else {                        System.out.print(arr[x]+ ", ");                 }          }          System.out.println("]");   }}

       (2)查找

           A:基本查找针对数组无序的情况

* 分析:

 *          A:定义最大索引,最小索引

 *          B:计算出中间索引

 *          C:拿中间索引的值和要查找的值进行比较

 *                 相等:就返回当前的中间索引

 *                 不相等:

 *                        大    左边找

 *                        小    右边找

 *          D:重新计算出中间索引

 *                 大    左边找

 *                        max= mid - 1;

 *                 小    右边找

 *                        min= mid + 1;

 *          E:回到B

public class ArrayDemo {       publicstatic void main(String[] args) {              //定义一个数组              int[]arr = {11,22,33,44,55,66,77};              //写功能实现              intindex = getIndex(arr, 33);              System.out.println("index:"+index);              //假如这个元素不存在后有什么现象呢?              index= getIndex(arr, 333);              System.out.println("index:"+index);       }       /*        * 两个明确:        * 返回值类型:int        * 参数列表:int[]arr,int value        */       publicstatic int getIndex(int[] arr,int value){              //定义最大索引,最小索引              intmax = arr.length -1;              intmin = 0;              //计算出中间索引              intmid = (max +min)/2;              //拿中间索引的值和要查找的值进行比较              while(arr[mid]!= value){                     if(arr[mid]>value){                            max= mid - 1;                     }elseif(arr[mid]<value){                            min= mid + 1;                     }                     //加入判断                     if(min> max){                            return-1;                     }                     mid= (max +min)/2;              }              return  mid;       }}

      B:二分查找(折半查找)针对数组有序的情况(不要先排序在查找)

public class ArrayDemo2 {       publicstatic void main(String[] args) {              //定义数组              int[]arr = { 24, 69, 80, 57, 13 };              //先排序              bubbleSort(arr);              //后查找              intindex = getIndex(arr, 80);              System.out.println("index:"+ index);       }       //冒泡排序代码       publicstatic void bubbleSort(int[] arr) {              for(int x = 0; x < arr.length - 1; x++) {                     for(int y = 0; y < arr.length - 1 - x; y++) {                            if(arr[y] > arr[y + 1]) {                                   inttemp = arr[y];                                   arr[y]= arr[y + 1];                                   arr[y+ 1] = temp;                            }                     }              }       }       //二分查找       publicstatic int getIndex(int[] arr, int value) {              //定义最大索引,最小索引              intmax = arr.length - 1;              intmin = 0;              //计算出中间索引              intmid = (max + min) / 2;              //拿中间索引的值和要查找的值进行比较              while(arr[mid] != value) {                     if(arr[mid] > value) {                            max= mid - 1;                     }else if (arr[mid] < value) {                            min= mid + 1;                     }                     //加入判断                     if(min > max) {                            return-1;                     }                     mid= (max + min) / 2;              }              return  mid;       }}                  

(3)Arrays工具类

              A:是针对数组进行操作的工具类。包括排序和查找等功能。

              B:要掌握的方法

                     把数组转成字符串:

public static String toString(int[] a)                    把数组转成字符串

                     排序:

public static void sort(int[] a)                            对数组进行排序

public static void bubbleSort(int[] arr)                冒泡排序

public static void selectSort(int[] arr)                  选择排序

                     二分查找:

public static int binarySearch(int[]arr,int value)  二分查找(折半查找)

public class ArraysDemo {       publicstatic void main(String[] args) {              //定义一个数组              int[]arr = { 24, 69, 80, 57, 13 };               //public static String toString(int[] a) 把数组转成字符串              System.out.println("排序前:" + Arrays.toString(arr));               //public static void sort(int[] a) 对数组进行排序              Arrays.sort(arr);              System.out.println("排序后:" + Arrays.toString(arr));               //[13, 24, 57, 69, 80]              //public static int binarySearch(int[] a,int key) 二分查找              System.out.println("binarySearch:"+ Arrays.binarySearch(arr, 57));              System.out.println("binarySearch:"+ Arrays.binarySearch(arr, 577));       }}

       (4)把字符串中的字符进行排序

              举例:"edacbgf"   得到结果       "abcdefg"

* 分析:

 *          A:定义一个字符串

 *          B:把字符串转换为字符数组

 *          C:把字符数组进行排序

 *          D:把排序后的字符数组转成字符串

 *          E:输出最后的字符串

public class ArrayTest {       publicstatic void main(String[] args) {              //定义一个字符串              Strings = "dacgebf";              //把字符串转换为字符数组              char[]chs = s.toCharArray();              //把字符数组进行排序              bubbleSort(chs);              //把排序后的字符数组转成字符串              Stringresult = String.valueOf(chs);                           //输出最后的字符串              System.out.println("result:"+result);       }       //冒泡排序       publicstatic void bubbleSort(char[] chs) {              for(int x = 0; x < chs.length - 1; x++) {                     for(int y = 0; y < chs.length - 1 - x; y++) {                            if(chs[y] > chs[y + 1]) {                                   chartemp = chs[y];                                   chs[y]= chs[y + 1];                                   chs[y+ 1] = temp;                            }                     }              }       }}

3:Integer(掌握)

       (1)为了让基本类型的数据进行更多的操作,Java就为每种基本类型提供了对应的包装类类型

              byte              Byte

              short              Short

              int                 Integer

              long               Long

              float               Float

              double            Double

              char               Character

              boolean          Boolean

* 用于基本数据类型与字符串之间的转换。

public class IntegerDemo {       publicstatic void main(String[] args) {              //不麻烦的就来了              //public static String toBinaryString(int i)              System.out.println(Integer.toBinaryString(100));              //public static String toOctalString(int i)              System.out.println(Integer.toOctalString(100));              // public static String toHexString(int i)              System.out.println(Integer.toHexString(100));              // public static final int MAX_VALUE              System.out.println(Integer.MAX_VALUE);              //public static final int MIN_VALUE              System.out.println(Integer.MIN_VALUE);       }}

       (2)Integer的构造方法

A:public Integer(int value)

                            Integeri = new Integer(100);

B:public Integer(String s)

                            Integeri = new Integer("100");

                     注意:这里的字符串必须是由数字字符组成

public class IntegerDemo {       publicstatic void main(String[] args) {              // 方式1              int i = 100;              Integer ii = new Integer(i);              System.out.println("ii:" + ii);              //方式2              Strings = "100";              //NumberFormatException              //String s = "abc";              Integeriii = new Integer(s);              System.out.println("iii:"+ iii);       }}

       (3)Stringint的相互转换

              A:String -- int

                     Integer.parseInt("100");

              B:int -- String

                     String.valueOf(100);

public class IntegerDemo {   publicstatic void main(String[] args) {          //int -- String          intnumber = 100;          //方式1          Strings1 = "" + number;          System.out.println("s1:"+ s1);          //方式2          Strings2 = String.valueOf(number);          System.out.println("s2:"+ s2);          //方式3          //int -- Integer -- String          Integeri = new Integer(number);          Strings3 = i.toString();          System.out.println("s3:"+ s3);          //方式4          //public static String toString(int i)          Strings4 = Integer.toString(number);          System.out.println("s4:"+ s4);          System.out.println("-----------------");          //String -- int          Strings = "100";          //方式1          //String -- Integer -- int          Integerii = new Integer(s);          //public int intValue()          intx = ii.intValue();          System.out.println("x:"+ x);          //方式2          //publicstatic int parseInt(String s)          inty = Integer.parseInt(s);          System.out.println("y:"+y);   }}

       (4)其他的功能(了解)

              常用的基本进制转换

 public static String toBinaryString(int i)

 public static String toOctalString(int i)

 public static String toHexString(int i)

十进制到其他进制

public static String toString(inti,int radix)

由这个我们也看到了进制的范围:2-36

为什么呢?0,...9,a...z

其他进制到十进制

public static int parseInt(String s,intradix)

public class IntegerDemo {    publicstatic void main(String[] args) {           //十进制到二进制,八进制,十六进制           System.out.println(Integer.toBinaryString(100));           System.out.println(Integer.toOctalString(100));           System.out.println(Integer.toHexString(100));           System.out.println("-------------------------");           // 十进制到其他进制           System.out.println(Integer.toString(100,10));           System.out.println(Integer.toString(100,2));           System.out.println(Integer.toString(100,8));           System.out.println(Integer.toString(100,16));           System.out.println(Integer.toString(100,5));           System.out.println(Integer.toString(100,7));           System.out.println(Integer.toString(100,-7));           System.out.println(Integer.toString(100,70));           System.out.println(Integer.toString(100,1));           System.out.println(Integer.toString(100,17));           System.out.println(Integer.toString(100,32));           System.out.println(Integer.toString(100,37));           System.out.println(Integer.toString(100,36));           System.out.println("-------------------------");           //其他进制到十进制           System.out.println(Integer.parseInt("100",10));           System.out.println(Integer.parseInt("100",2));           System.out.println(Integer.parseInt("100",8));           System.out.println(Integer.parseInt("100",16));           System.out.println(Integer.parseInt("100",23));           //NumberFormatException           //System.out.println(Integer.parseInt("123",2));    }}

       (5)JDK5的新特性

              自动装箱       基本类型--引用类型

              自动拆箱       引用类型--基本类型

              * 注意一个小问题:

             *           在使用时,Integer  x = null;代码就会出现NullPointerException。

             *           建议先判断是否为null,然后再使用。

              把下面的这个代码理解即可:

                     Integer i = 100;

                     i += 200;

public class IntegerDemo {       publicstatic void main(String[] args) {              //定义了一个int类型的包装类类型变量i              //Integer i = new Integer(100);              Integer ii = 100;              ii += 200;              System.out.println("ii:"+ ii);              // 通过反编译后的代码              // Integer ii =Integer.valueOf(100); //自动装箱              // ii =Integer.valueOf(ii.intValue() + 200); //自动拆箱,再自动装箱              // System.out.println((newStringBuilder("ii:")).append(ii).toString());              Integeriii = null;              //NullPointerException              if(iii != null) {                     iii+= 1000;                     System.out.println(iii);              }       }}

       (6)面试题

              -128127之间的数据缓冲池问题

public class IntegerDemo {       publicstatic void main(String[] args) {              Integeri1 = new Integer(127);              Integer i2 = new Integer(127);              System.out.println(i1 == i2);              System.out.println(i1.equals(i2));              System.out.println("-----------");              Integer i3 = new Integer(128);              Integer i4 = new Integer(128);              System.out.println(i3 == i4);              System.out.println(i3.equals(i4));              System.out.println("-----------");              Integer i5 = 128;              Integer i6 = 128;              System.out.println(i5 == i6);              System.out.println(i5.equals(i6));              System.out.println("-----------");              Integer i7 = 127;              Integer i8 = 127;              System.out.println(i7 == i8);              System.out.println(i7.equals(i8));// 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间              //Integer ii = Integer.valueOf(127);       }}

4:Character(了解)

Character 类在对象中包装一个基本类型 char 的值

 * 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然   

(1)Character构造方法  

              Character ch = new Character('a');

public class CharacterDemo {   publicstatic void main(String[] args) {          //创建对象          //Character ch = new Character((char) 97);          Characterch = new Character('a');          System.out.println("ch:"+ ch);   }}

       (2)要掌握的方法:

 * public static booleanisUpperCase(char ch):                 判断给定的字符是否是大写字符

 * public static booleanisLowerCase(char ch):                 判断给定的字符是否是小写字符

 * public static booleanisDigit(char ch):                          判断给定的字符是否是数字字符

 * public static chartoUpperCase(char ch):                      把给定的字符转换为大写字符

 * public static chartoLowerCase(char ch):                      把给定的字符转换为小写字符   

public class CharacterDemo {       publicstatic void main(String[] args) {              //public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符              System.out.println("isUpperCase:"+ Character.isUpperCase('A'));              System.out.println("isUpperCase:"+ Character.isUpperCase('a'));              System.out.println("isUpperCase:"+ Character.isUpperCase('0'));              System.out.println("-----------------------------------------");              //public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符              System.out.println("isLowerCase:"+ Character.isLowerCase('A'));              System.out.println("isLowerCase:"+ Character.isLowerCase('a'));              System.out.println("isLowerCase:"+ Character.isLowerCase('0'));              System.out.println("-----------------------------------------");              //public static boolean isDigit(char ch):判断给定的字符是否是数字字符              System.out.println("isDigit:"+ Character.isDigit('A'));              System.out.println("isDigit:"+ Character.isDigit('a'));              System.out.println("isDigit:"+ Character.isDigit('0'));              System.out.println("-----------------------------------------");              //public static char toUpperCase(char ch):把给定的字符转换为大写字符              System.out.println("toUpperCase:"+ Character.toUpperCase('A'));              System.out.println("toUpperCase:"+ Character.toUpperCase('a'));              System.out.println("-----------------------------------------");              //public static char toLowerCase(char ch):把给定的字符转换为小写字符              System.out.println("toLowerCase:"+ Character.toLowerCase('A'));              System.out.println("toLowerCase:"+ Character.toLowerCase('a'));       }}

       (3)案例:

              统计字符串中大写,小写及数字字符出现的次数

* 分析:

 *             A:定义三个统计变量。

 *                    intbigCont=0;

 *                    intsmalCount=0;

 *                    intnumberCount=0;

 *             B:键盘录入一个字符串。

 *             C:把字符串转换为字符数组。

 *             D:遍历字符数组获取到每一个字符

 *             E:判断该字符是

 *                    大写       bigCount++;

 *                    小写       smalCount++;

 *                    数字       numberCount++;

 *             F:输出结果即可

public class CharacterTest {   publicstatic void main(String[] args) {          //定义三个统计变量。          intbigCount = 0;          intsmallCount = 0;          intnumberCount = 0;          //键盘录入一个字符串。          Scannersc = new Scanner(System.in);          System.out.println("请输入一个字符串:");          Stringline = sc.nextLine();          //把字符串转换为字符数组。          char[]chs = line.toCharArray();          //历字符数组获取到每一个字符          for(int x = 0; x < chs.length; x++) {                 charch = chs[x];                 //判断该字符                 if(Character.isUpperCase(ch)) {                        bigCount++;                 }else if (Character.isLowerCase(ch)) {                        smallCount++;                 }else if (Character.isDigit(ch)) {                        numberCount++;                 }          }          //输出结果即可          System.out.println("大写字母:" + bigCount + "个");          System.out.println("小写字母:" + smallCount + "个");          System.out.println("数字字符:" + numberCount + "个");   }}

原创粉丝点击