2017

来源:互联网 发布:mac地址表老化时间 编辑:程序博客网 时间:2024/04/30 18:54
1 StringBuffer
  我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的String对象,既耗时,又浪费空间。而StringBuffer就可以解决这个问题。
-----------------------
线程安全(多线程时讲解):
安全 -- 同步 --- 数据是安全的
不安全 -- 不同步 --- 效率高一些
安全和效率问题时永远困扰我们的问题。。。    
安全:医院的网站,银行网站
效率:新闻网站,论坛之类的。
-----------------------
StringBuffer:线程安全的可变字符串。

StringBuffer和String的区别?
前者长度和内容可变,后者不可变。
如果使用前者做字符串的拼接,不会浪费大多的资源。

2 StringBuffer 的构造方法
   public StringBuffer():无参构造
   public StringBuffer(int capacity):指定容量的字符串缓冲区对象
   public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
  StringBuffer的方法:
   public int capacity():返回当前容量   理论值
   public int length():返回长度(字符数) 实际值
-----------------------------------------------
   StringBuffer sb = new StringBuffer();
   System.out.println("sb:"+sb); //sb:
   System.out.println("sb.capacity():"+sb.capacity()); //sb.capacity():16
   System.out.println("sb.length:"+sb.length());//sb.length:0
-----------------------------------------------  
   StringBuffer sb2 = new StringBuffer(50);
   System.out.println("sb2:"+sb2); //sb2:
   System.out.println("sb2.capacity():"+sb2.capacity()); //sb2.capacity():50
   System.out.println("sb2.length:"+sb2.length());//sb2.length:0
------------------------------------------------
   StringBuffer sb3 = new StringBuffer("hello");
   System.out.println("sb3:"+sb3); //sb3:hello

   System.out.println("sb3.capacity():"+sb3.capacity()); //sb3.capacity():21   **21=16+5  默认16                          

   System.out.println("sb3.length:"+sb3.length());//sb3.length:5

3 StringBuffer的添加功能
  public StringBuffer append(String str):可以把任意类型添加到字符串缓冲区里面,并返回字符串缓冲区本身
  public StringBuffer insert(int offset,String str)在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
-----------------------
   StringBuffer sb = new StringBuffer();
   StringBuffer sb2 = sb.append("hello");
   System.out.println("sb:"+sb); //sb:hello
   System.out.println("sb2:"+sb); //sb2:hello
   System.out.println(sb==sb2); true
------------------------
   StringBuffer sb = new StringBuffer();
   sb.append("hello");
   System.out.println("sb:"+sb); //sb:hello
------------------------
   //一步一步添加数据
   sb.append("hello");
   sb.append(true);
   sb.append(12);
   sb.append(34.56);
-------------------------
   //链式编程(返回StringBuffer)
   sb.append("hello").append(true).append(12).append(34.56);
-------------------------

4 StringBuffer的删除功能
  public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
  public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身

  //删除所有的数据
  sb.delete(0,sb.length());

5 StringBuffer的替换功能
   public StringBuffer replace(int start,int end,string str):从stat开始到end结束用str替换   包左不包右

   sb.append("hello");
   sb.append("world");
   sb.append("java");
   System.out.println("sb:"+sb);//helloworldjava
   sb.replace(5,10,"节日快乐");
   System.out.println("sb:"+sb);//hello节日快乐java

6 StringBuffer的反转功能
  public StringBuffer reverse()

7 StringBuffer的截取功能
  public String substring(int start)
  public String substring(int start,int end)

  sb.append("hello").append("world").append("java");
  System.out.println("sb:"+sb);
  String s = sb.substring(5);
  System.out.println("s:"+s);
  System.out.println("sb:"+sb);
  //sb:helloworldjava
  //s:worldjava
  //sb:helloworldjava    sb没有变,因为返回的是string

8 StringBuffer和String的转换
  为什么要类型转换
  A--B的转换
  把A转换成B,其实是为了使用B的功能
  B--A的转换
  把B转成A,可能要的结束时A类型,所以又转换回来

  String --- StringBuffer
  String s ="hello";
  方式1:通过构造方法
  StringBuffer sb = new StringBuffer(s);
  方式2:通过append()方法
  StringBuffer sb2 = new StringBuffer();
  sb2.append(s);
  
  StringBuffer --- String
  StringBuffer buffer = new StringBuffer("java");
  方式1:通过构造方法
  String str = new String(buffer);
  方式2:通过toString()方法  //StringBuffer重写之后 返回String
  String str2 = buffer.toString()

9 用StringBuffer做拼接
  public Static String arrayToString2(int[] arr){
        StringBuffer sb = 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("}");
         return sb.toString();
  }
10 判断一个字符串是否是对称字符串
     第一个和最后一个比较
     第二个和倒数第二个比较
      。。。
     比较的次数是长度除以2

A:    public static boolean isSame(String s){
       boolean flag = 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;
   }
B: 字符串缓冲区的反转功能
       public static boolean isSame2(String s){
           return new StringBuffer(s).reverse().toString().equals(s);  //StringBuffer 没有equals方法所以使用的是Object类的 
 }
***11 String、StringBuffer、StringBuilder 几个面试题 
  1、String,StringBuffer,StringBuilder的区别  
  A:String是内容不可变的,而StringBuffer和StringBuilder是内容可变的
  B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高
  2、StringBuffer和数组的区别
  两者都可以看成是一个容器,装其它的数据
  但是,StringBuffer的数据始终是一个字符串数据
  而数组可以放置多种数据,但必须是同一种类型的
  3、形式参数的问题
     String作为参数传递
     StringBuffer作为参数传递
  形式参数:
     基本类型:形式参数的改变不影响实际参数
     引用类型:形式参数的改变直接影响实际参数 
  **注意:
    String作为参数传递,效果和基本类型作为参数传递是一样的。
  public static void main(String[] args){
  String s1 = "hello";
  String s2 = "world";
  System.out.println(s1+"---"+s2); //hello---world  
  change(s1,s2);
  System.out.println(s1+"---"+s2); //hello---world

  StringBuffer sb1 = new StringBuffer("hello");  
  StringBuffer sb2 = new StringBuffer("world");
  System.out.println(sb1+"---"+sb2);//hello---world
  change(sb1,sb2);
  System.out.println(sb1+"---"+sb2);//***hello---worldworld
}
  private static void change(StringBuffer sb1,StringBuffer sb2){
          sb1 = sb2;
          sb2.appned(sb1);
  }
  
  public static void change(String s1,String s2){
          s1 = s2;
          s2 = s1+s2;
   }
----输出 hello---worldworld------可以看一下
 https://zhidao.baidu.com/question/646187979248073005.html
12 数组排序与查找
(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]){
                 int temp = arr[y];
                 arr[y] = arr[y+1];
                 arr[y+1] = temp;
               } 
    } 
(2) 选择排序
      从0索引处开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
    for(int x=0;x<arr.length-1;x++){
         for(int y=x+1;y<arr.length;y++){
            if(arr[y]<arr[x]){
              int temp =arr[x];
              arr[x]=arr[y];
              arr[y]=temp;
      }
 }
(3)二分查找
  基本查找:数组元素无需(从头找到尾)
  二分查找(折半查找):数组元素有序
  public static int getIndex(int[] arr,int value){
         //定义最大索引,最小索引
         int max = arr.length - 1;
         int min = 0;
         //计算出中间索引
         int mid = (max+min)/2;
         //拿中间索引的值和要查找的值进行比较
         while(arr[mid]!=value){
              if(arr[mid]>value){
                  max =mid -1;
           }else if(arr[mid]<value){
               min = mid+1;
           }
         mid = (max+min)/2;
     }
   return mid;
}
  注意:如果数组是无序的,不能用二分查找
        因为如果先排序,就会把数组元素中的原始位置改变。
        
13 Arrays
   java.util  jdk1.2
   此类包含用来操作数组(比如排序和查找)的各种方法。
   全是静态方法。
(1)public static String toString(int[] a):把数组转换成字符串  jdk1.5
(2)public static void sort(int[] a):对数组进行排序(快排)
(3)public static int binarySearch(int[] a,int key):二分查找
--------------------------------   
 int[] arr = {24,69,80,57,13};
 System.out.println("排序前:"+Arrays.toString(arr));//输出: 排序前:[24, 69, 80, 57, 13]
 Arrays.sort(arr);
 System.out.println("排序后:"+Arrays.toString(arr));//输出: 排序前:[13, 24, 57, 69 ,80]
 System.out.println("binarySearch:"+Arrays.binarySearch(arr,57));//输出:binarySearch:2

14 基本类型包装的引入

  需求1:要求把100这个数据的二进制,八进制,十六进制计算出来
  需求2:要求判断一个数据是否是int范围内的

  用Integer类
  A:// 二进制转换
    System.out.println(Integer.toBinaryString(100)); //1100100
    // 八进制转换
    System.out.println(Integer.toOctalString(100)); //144
    //十六进制 

    System.out.println(Integer.toHexString(100));  //64

  B:System.out.println(Integer.MAX_VALUE);  2的31次方-1// 2147483647
    System.out.println(Integer.MIN_VALUE);  -2的31次方//-2147483648
------------------------
    为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了相应的类类型。包装类型。
   byte       Byte
   short      Short
   int        Integer
   long       Long
   float      Float
   double     Double
   char       Character
   boolean    Boolean

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

15 Integer
(1)构造方法
      public Integer(int value)
      public Integer(String s)

 //方法1
 int i=100;
 Integer ii = new Integer(i);
 System.out.println("ii:"+ii); //ii:100

 //方式2
 String s = "100";
 //String s ="abc";// 这个会报错,必须要由全部是数字组成的
 Integer iii = new Integer(s);
 System.out.println("iii:"+iii);//iii:100

(2)int类型和String类型的转换
   int---String
   int number = 100;
   //方式1 
   String s1 = ""+number;
   System.out.println("s1:"+s1); //s1:100
   //方式2 推荐使用
   String s2 = String.valueOf(number);
   System.out.println("s2:"+s2);
   //方式3  最差的。。。
   //int -- Integer -- String  最差的。。。
   Integer i = new Integer(number);
   String s3 =i.toString();
   System.out.println("s3:"+s3);
   //方式4
   String s4 = Integer.toString(number);
   System.out.println("s4:"+s4);
   
   String---int
   String s ="100";
   //方式1
   //String -- Integer -- int
   Integer li =new Integer(s);
   int x =li.intValue();
***//方式2  推荐使用
   int y = Integer.parseInt(s)

(3)进制转换
   public static String toBinaryString(int i) //二进制
   public static String toOctalString(int i)  // 八进制
   public static String toHexString(int i)   // 十六进制
   
**A:十进制到其他进制
   public static String toString(int i,int radix)
   例如:
   System.out.println(Integer.toString(100,5));//400 五进制  
   System.out.println(Integer.toString(100,-7));// 100 进制不能是负的
   System.out.println(Integer.toString(100,70));//100 进制不能是70
-------------------经过一些测试--------进制范围为 2-36 ------
   因为 0--9 a--z 一共有36个

**B:其他进制到十进制
   public static int parseInt(String s , int radix)
   例如:
   System.out.println(Integer.parseInt("100",10))//100  十进制100
   System.out.println(Integer.parseInt("100",2))//4   二进制100

16 jdk5新特性 自动装箱和拆箱
   自动装箱:把基本类型转换成包装类类型
   自动拆箱:把包装类类型转换成基本类型

   //定义了一个int类型的包装类类型变量i
   //Integer i = new Integer(100);
   Integer li =100;
   li +=200;
   System.out.println("li:"+li);

   //通过反编译
   //Integer li = Integer.valueOf(100);   //自动装箱---把int类型变成Integer
   //li = Integer.valueOf(li.intValue()+200);//自动拆箱,再自动装箱---返回li的int类型100与200相加后又转换成Integer类型
   //System.out.println((new StringBuilder("li:")).append(li).toString())

***注意:
     在使用时,Integer  x = null;上面的代码就会出现NullPointerException。

   Integer iii = null;
   if(iii!=null){
        iii += 100;
        System.out.println(iii);
  }

17 Integer的面试题
(1) Integer i =1;i+=1; 做了哪些事情
    先是自动装箱,然后自动拆箱,然后自动装箱
(2) 
    Integer i1 = new Integer(127);
    Integer i2 = new Integer(127);
    System.out.println(i1==i2);             //false
    System.out.println(i1.equals(i2));      //true
   
    Integer i3 = new Integer(128);
    Integer i4 = new Integer(128);
    System.out.println(i3==i4);             //false
    System.out.println(i3.equals(i4));      //true
  
    Integer i5 = 128;
    Integer i6 = 128;
    System.out.println(i5==i6);             //false
    System.out.println(i5.equals(i6));      //true

    Integer i7 = 127;
    Integer i8 = 127;
    System.out.println(i7==i8);             //true
    System.out.println(i7.equals(i8));      //true

    //通过查看源码,可以得知,针对-128到127之间的数据,做了一个数据缓冲器,如果对象是该范围内的,每次并不创建新的空间。
    注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池获取数据。

18 Character
A: Character类在对象中包装了一个基本类型char的值
   该类,提供了几种方法,以确定字符的类别(小写字母、数字等等),并将字符从大写转换成小写,反之同理。
   构造方法:
    Character(char value);
   例如:
   Character ch 
B: 方法
public static boolean isUpperCase(char ch): 判断给定的字符是否是大写字符
public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符
public static boolean isDigit(char ch):判断给定的字符是否是数字字符
public static char toUpperCase(char ch):将给定的字符转换成大写字符
public static char toLowerCase(char ch):将给定的字符转换成小写字符

 例如:
  System.out.println("isUpperCase:"+Character.isUpperCase('A'));//true
  System.out.println("toUpperCase:"+Character.toUpperCase('A'));//a

C:统计字符串中大写小写以及数字出现的次数
  //定义三个统计变量
  int bigCount = 0;
  int smallCount = 0;
  int numberCount =0;
  //键盘录入一个字符串
  Scanner sc = new Scanner(System.in);
  System.out.println("请输入一个字符串:");
  String line = sc.nextLine();
  //把字符串转换为字符数组 
  char[] chs = line.toCharArray();
  //遍历字符数组获取到每一个字符
  for(int x =0;x<chs.length();x++){
    char ch = chs[x];
   //判断该字符
   if(Character.isUpperCase(ch)){
      bigCount++;
     }else if(Character.isLowerCase(ch)){
        smallCount++;
   }else if(Character.isDigit(ch)){
           numberCount++;
      }
 }  

原创粉丝点击