java常用类-StringBuffer,Integer,Character

* StringBuffer:
* 线程安全的可变字符串。
*
* StringBuffer和String的区别?
* 前者长度和内容可变,后者不可变。
* 如果使用前者做字符串的拼接,不会浪费太多的资源。
*
* StringBuffer的构造方法:
* public StringBuffer():无参构造方法
* public StringBuffer(int capacity):指定容量的字符串缓冲区对象
* public StringBuffer(String str):指定字符串内容的字符串缓冲区对象
*
* StringBuffer的方法:
* public int capacity():返回当前容量。 理论值
* public int length():返回长度(字符数)。 实际值

         // public StringBuffer():无参构造方法
StringBuffer sb = 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):指定容量的字符串缓冲区对象
StringBuffer sb2 = 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):指定字符串内容的字符串缓冲区对象
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("sb3:" + sb3);
System.out.println("sb3.capacity():" + sb3.capacity());
System.out.println("sb3.length():" + sb3.length());

* StringBuffer的添加功能:
* public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
*
* public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身

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

// 需求:我要删除所有的数据
sb.delete(0, sb.length());

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

* StringBuffer的反转功能:
* public StringBuffer reverse()

* StringBuffer的截取功能:注意返回值类型不再是StringBuffer本身了
* public String substring(int start)
* public String substring(int start,int end)

* String和StringBuffer的相互转换:

         // String -- StringBuffer
String s = "hello";
// 注意:不能把字符串的值直接赋值给StringBuffer
// StringBuffer sb = "hello";
// StringBuffer sb = s;
// 方式1:通过构造方法
StringBuffer sb = new StringBuffer(s);
// 方式2:通过append()方法
StringBuffer sb2 = new StringBuffer();
sb2.append(s);
System.out.println("sb:" + sb);
System.out.println("sb2:" + sb2);
System.out.println("---------------"); // StringBuffer -- String
StringBuffer buffer = new StringBuffer("java");
// String(StringBuffer buffer)
// 方式1:通过构造方法
String str = new String(buffer);
// 方式2:通过toString()方法
String str2 = buffer.toString();
System.out.println("str:" + str);
System.out.println("str2:" + str2);

* 把数组拼接成一个字符串

     // 用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();
}

* 面试题:
* 1:String,StringBuffer,StringBuilder的区别?
* A:String是内容不可变的,而StringBuffer,StringBuilder都是内容可变的。
* B:StringBuffer是同步的,数据安全,效率低;StringBuilder是不同步的,数据不安全,效率高
*
* 2:StringBuffer和数组的区别?
* 二者都可以看出是一个容器,装其他的数据。
* 但是呢,StringBuffer的数据最终是一个字符串数据。
* 而数组可以放置多种数据,但必须是同一种数据类型的。
*
* 3:形式参数问题
* String作为参数传递
* StringBuffer作为参数传递
*
* 形式参数:
* 基本类型:形式参数的改变不影响实际参数
* 引用类型:形式参数的改变直接影响实际参数
*
* 注意:
* String作为参数传递,效果和基本类型作为参数传递是一样的。

冒泡排序图解:

java常用类-StringBuffer,Integer,Character

 public class BubbleSort {
public static void sort(int[] data) {
for (int i = 0; i < data.length - 1; i++) {
for (int j = 0; j < data.length - 1 - i; j++) {
if (data[j] > data[j + 1]) {
SortTest.swap(data, j, j + 1);
}
}
}
}
}

选择排序图解:

java常用类-StringBuffer,Integer,Character

 public class SelectionSort {
public static void sort(int[] data) {
for (int x = 0; x < data.length - 1; x++) {
for (int y = x + 1; y < data.length; y++) {
if (data[y] < data[x]) {
SortTest.swap(data, x, y);
}
}
}
}
}

二分查找图解:

java常用类-StringBuffer,Integer,Character

     // 二分查找
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;
} // 加入判断
if (min > max) {
return -1;
} mid = (max + min) / 2;
} return mid;
}

* Arrays:针对数组进行操作的工具类。比如说排序和查找。
* 1:public static String toString(int[] a) 把数组转成字符串
* 2:public static void sort(int[] a) 对数组进行排序
* 3:public static int binarySearch(int[] a,int key) 二分查找

Arrays工具类的方法源码解析:

 public static String toString(int[] a)
public static void sort(int[] a) 底层是快速排序,知道就可以了。有空看,有问题再问我
public static int binarySearch(int[] a,int key) 开发原则:
只要是对象,我们就要判断该对象是否为null。 int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:" + Arrays.toString(arr)); public static String toString(int[] a) {
//a -- arr -- { 24, 69, 80, 57, 13 } if (a == null)
return "null"; //说明数组对象不存在
int iMax = a.length - 1; //iMax=4;
if (iMax == -1)
return "[]"; //说明数组存在,但是没有元素。 StringBuilder b = new StringBuilder();
b.append('['); //"["
for (int i = 0; ; i++) {
b.append(a[i]); //"[24, 69, 80, 57, 13"
if (i == iMax)
//"[24, 69, 80, 57, 13]"
return b.append(']').toString();
b.append(", "); //"[24, 69, 80, 57, "
}
}
----------------------------------------------------- int[] arr = {13, 24, 57, 69, 80};
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577)); public static int binarySearch(int[] a, int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//key -- 577
return binarySearch0(a, 0, a.length, key);
} private static int binarySearch0(int[] a, int fromIndex, int toIndex,
int key) {
//a -- arr -- {13, 24, 57, 69, 80}
//fromIndex -- 0
//toIndex -- 5
//key -- 577 int low = fromIndex; //low=0
int high = toIndex - 1; //high=4 while (low <= high) {
int mid = (low + high) >>> 1; //mid=2,mid=3,mid=4
int midVal = a[mid]; //midVal=57,midVal=69,midVal=80 if (midVal < key)
low = mid + 1; //low=3,low=4,low=5
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}

* 为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了对应的类类型。包装类类型。
* byte Byte
* short Short
* int Integer
* long Long
* float Float
* double Double
* char Character
* boolean Boolean
*
* 用于基本数据类型与字符串之间的转换。

         // 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);

* Integer的构造方法:
* public Integer(int value)
* public Integer(String s)
* 注意:这个字符串必须是由数字字符组成

         String s = "100";
// NumberFormatException
// String s = "abc";
Integer iii = new Integer(s);
System.out.println("iii:" + iii);

* int类型和String类型的相互转换
*
* int -- String
* String.valueOf(number)
*
* String -- int
* Integer.parseInt(s)

         // int -- String
int number = 100;
// 方式1
String s1 = "" + number;
System.out.println("s1:" + s1);
// 方式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
// public static String toString(int i)
String s4 = Integer.toString(number);
System.out.println("s4:" + s4);
System.out.println("-----------------"); // String -- int
String s = "100";
// 方式1
// String -- Integer -- int
Integer ii = new Integer(s);
// public int intValue()
int x = ii.intValue();
System.out.println("x:" + x);
//方式2
//public static int parseInt(String s)
int y = Integer.parseInt(s);
System.out.println("y:"+y);

* 常用的基本进制转换
* public static String toBinaryString(int i)
* public static String toOctalString(int i)
* public static String toHexString(int i)
*
* 十进制到其他进制
* public static String toString(int i,int radix)
* 由这个我们也看到了进制的范围:2-36
* 为什么呢?0,...9,a...z
*
* 其他进制到十进制
* public static int parseInt(String s,int radix)

* JDK5的新特性
* 自动装箱:把基本类型转换为包装类类型
* 自动拆箱:把包装类类型转换为基本类型
*
* 注意一个小问题:
* 在使用时,Integer x = null;代码就会出现NullPointerException。
* 建议先判断是否为null,然后再使用。

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

* 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据

         Integer i5 = 128;
Integer i6 = 128;
System.out.println(i5 == i6); //false
System.out.println(i5.equals(i6)); //true
System.out.println("-----------"); Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8); //true
System.out.println(i7.equals(i8)); //true

* Character 类在对象中包装一个基本类型 char 的值
* 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然
*
* 构造方法:
* Character(char value)

* 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):把给定的字符转换为小写字符

上一篇:Intent四个重要属性


下一篇:我、实战nginx+naxsi(WAF)之一