day13
2018-05-18 本文已影响19人
775444e2a800
package com.heima.test;
import java.util.Arrays;
//01-08 Stringbuffer相关
//09 Arrays相关
public class Test13 {
public static void main(String[] args) {
// demo01(); length capacity
// demo02(); append insert
// demo03(); delete deleteCharAt
// demo04(); replace reverse
// demo05(); substring
// demo06(); 字符串转Stringbuffer:append 构造方法
// demo07(); Stringbuffer转字符串:String构造 直接打印引用 截取方法
// demo08(); Stringbuffer和Stringbiuder和String的区别
// 具体可看13.11
// 基本数据类型的值传递,不改变其值.
// 引用数据类型的值传递,改变其值.(String虽然是引用数据类型,但是不改变值)
// demo09(); Arrays的 tostring sort binarysearch
// 基本数据类型的包装类
// byte=Byte short=Short int=Integer long=Long float=Float double=Double char=Character boolean=Boolean
// demo10(); Integer包装类
// demo11(); int转String String转int
}
public static void demo11() {
//int转String 4种方法
//a
int i=100;
String str=i+"";
System.out.println(str);
//b
int i2=200;
String str2=String.valueOf(i2);
System.out.println(str2);
//c
int i3=300;
Integer in=new Integer(i3);
String str3=in.toString();
System.out.println(str3);
//d
int i4=400;
String str4=Integer.toString(i4);
System.out.println(str4);
//String转int两种方法
//a
Integer in2=new Integer("500");
int in3=in2.intValue();
System.out.println(in3);
//b
int in4=Integer.parseInt("600");
System.out.println(in4);
}
public static void demo10() {
Integer in=new Integer(100);
System.out.println(in);
Integer in2=new Integer("100");
System.out.println(in2);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);
}
public static void demo09() {
int[] i={11,453,33,9,42,91};
//Arrays.toString int数组转字符串
String str= Arrays.toString(i);//[11, 22, 33, 44, 55, 66]
System.out.println(str);
//Arrays.sort int数组排序
Arrays.sort(i);//sort没有返回值
System.out.println(i);//[I@29ce8c
String str2=Arrays.toString(i);//[9, 11, 33, 42, 91, 453]
System.out.println(str2);
//Arrays.binarySearch int数组二分查找(前提是:需要已经排序好的数组)
System.out.println(Arrays.binarySearch(i,9));//0
System.out.println(Arrays.binarySearch(i,33));//2
System.out.println(Arrays.binarySearch(i,91));//4
System.out.println(Arrays.binarySearch(i,999));//-7(负的插入点-1)
}
public static void demo08() {
/*
Stringbuffer和Stringbiuder的区别:
1.前者为jdk1.0版本的,后者为jdk1.5版本
2.前者线性安全,后者线性不安全
String和以上两者的区别:
1.前者不可变字符序列,后者可变字符序列
* */
}
public static void demo07() {
//string的构造转换
StringBuffer sb=new StringBuffer("zhangyan");
String str=new String(sb);//zhangyan
System.out.println(str);
//Stringbuffer的截取转换
StringBuffer sb2=new StringBuffer("yanjing");
String str2=sb2.substring(0,sb2.length());//yanjing
System.out.println(str2);
//直接打印Stringbuffer的引用
StringBuffer sb3=new StringBuffer("lisi");//lisi
System.out.println(sb3);
}
public static void demo06() {
//append 字符串转Stringbuffer
StringBuffer sb=new StringBuffer();
sb.append("zhangyan");//zhangyan
System.out.println(sb);
//构造方法 字符串转Stringbuffer
StringBuffer sb2=new StringBuffer("yanjin");
System.out.println(sb2);//yanjin
}
public static void demo05() {
//substring 截取下表后字符
StringBuffer sb=new StringBuffer("zhangyan");
String str=sb.substring(5);//yan
System.out.println(str);
//substring 开头截取到指定下标(包含头不包含尾
StringBuffer sb2=new StringBuffer("zhangyan");
String str2=sb.substring(0,5);//zhang
System.out.println(str2);
}
public static void demo04() {
StringBuffer sb=new StringBuffer();
sb.append("abcde");
//replace
sb.replace(0,2,"AA");//AAcde
System.out.println(sb);
//reverse
sb.reverse();//edcAA
System.out.println(sb);
}
public static void demo03() {
StringBuffer sb=new StringBuffer();
sb.append("abcde");
//deleteCharAt
StringBuffer dsb=sb.deleteCharAt(1);//acde
System.out.println(dsb);
//delete
StringBuffer dasb=sb.delete(0,2);//de
System.out.println(dasb);
//delete清空数据区
StringBuffer dallsb=sb.delete(0,sb.length());
System.out.println(dallsb);
}
public static void demo02() {
//append
StringBuffer sb=new StringBuffer();
StringBuffer sb2=sb.append("abc");
StringBuffer sb3=sb.append(10);
StringBuffer sb4=sb.append(true);
System.out.println(sb);//abc10true
//inster
StringBuffer sb5=sb.insert(3,"AAA");
System.out.println(sb5);//abcAAA10true
}
public static void demo01() {
/*
* 三种实例化StringBuffer的方法
* */
StringBuffer sb=new StringBuffer();
int i=sb.length();//0 实际字符数
int i2=sb.capacity();//16 容器初始容量
System.out.println(i);
System.out.println(i2);
StringBuffer sb2=new StringBuffer("zhangsan");
int i3=sb2.length();//8 实际字符数
int i4=sb2.capacity();//24 容器初始容量+实际字符数
System.out.println(i3);
System.out.println(i4);
StringBuffer sb3=new StringBuffer(10);
int i5=sb3.length();//0
int i6=sb3.capacity();//10
System.out.println(i5);
System.out.println(i6);
}
}