***********************************String*****************************************************
构造方法:也就是说,在字节和字符中,关于string的构造方法,都会转码
/* * String类中的构造函数 * String(); 构造一个空字符串对象 * String(byte[] bytes); 通过byte数组构造字符串对象 * String(byte[] bytes,int offset,int length);(字节数组,开始的数据,截得数据长度) * String(byte[] bytes, int offset, int length, Charset charset) * String(char[] value); 通过char数组构造字符串对象 * String(byte[] char,int offset,int length);(字符数组,开始的数据,截得数据长度) * String(String original); 构造一个original的副本,拷贝一个original * String(StringBuffer buffer);通过StringBuffer数组构造字符串对象 * String(StringBuilder builder) */ private static void method01() { //请留意这里面的差别 byte[] bArray = { ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘}; System.out.println(new String(bArray));//打印出:abcdefgh byte[] byteArray = new byte[] {87, 79, 87, 97, 46, 46, 46}; System.out.println(new String(byteArray));//打印出:WOWa... String strbIndex = new String(byteArray, 1, 2); System.out.println(strbIndex);//打印出:OW System.out.println("上面字节数组,下面字符数组"); // 字符数组 char[] cArray = { ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘ }; System.out.println(new String(cArray));//打印出:abcdefgh char[] cArray2 = new char[]{87, 79, 87, 97, 46, 46, 46}; System.out.println(new String(cArray2));//注意这两种方法的区别。打印出:WOWa... System.out.println(String.valueOf(cArray));//注意这两种方法的区别。打印出:abcdefgh System.out.println(new String(cArray, 1, 2));//打印出:bc System.out.println("上面字符数组,下面StringBuffer"); //声明一个StringBuffer StringBuffer strbuf = new StringBuffer("strbuf"); String strbuff = new String(strbuf); System.out.println(strbuff);//打印出:strbuf }
private static void method03() { String str01 = new String("abc"); String str02 = new String("abc"); String str03 = "abc"; String str04 = "abc"; String str05 = "a" + "bc"; String str06 = "a"; String str08 = str06 + "bc"; System.out.println(str08 == str04);//false System.out.println(str04 == str05);//true常亮优化机制 System.out.println(str01 == str02);//false System.out.println(str01 == str03);//false System.out.println(str03 == str04);//true System.out.println(str01.equals(str02));//true System.out.println(str01.equals(str03));//true }
//字符串反转 private static void method02() { //方法一:利用方法 StringBuffer stringBuffer = new StringBuffer("123456"); stringBuffer.reverse(); System.out.println(stringBuffer.toString()); //方法二:利用算法 String str = "ABCDEFG"; char[] chars = str.toCharArray(); char[] rev = new char[chars.length]; for (int i = 0; i < chars.length -1; i++) { rev[i] = chars[chars.length - 1 - i]; } // String str2 = new String(rev); 两种字符数组转换成都可以 String str2 = String.valueOf(rev); System.out.println(str2); }
private static void method01() { String s = "afs123fdsa"; //使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。 byte[] b = s.getBytes(); for (int i = 0; i < b.length; i++) { byte c = b[i]; System.out.print(c+"、"); } System.out.println();//打印出:97、102、115、49、50、51、102、100、115、97、 String t = new String(b);//bytep[]转换为String,构造方法里面是字节数组或者字符数组,要转码 System.out.println(t);//打印出:afs123fdsa
/* * 此应该是常识:java默认GBK编码格式 */ private static void method03() throws UnsupportedEncodingException { byte[] b_default = "中".getBytes();//系统默认的编码格式。这里默认java的默认编码格式gbk byte[] b_gbk = "中".getBytes("GBK");//指定编码格式 byte[] b_utf8 = "中".getBytes("UTF-8"); byte[] b_iso88591 = "中".getBytes("ISO8859-1"); System.out.println(b_default.length); System.out.println(b_gbk.length);//打印出:2。通过gbk转码,一个汉字的长度是2个字节 System.out.println(b_utf8.length);//打印出:3。通过utf-8转码,一个汉字的长度是3个字节 System.out.println(b_iso88591.length);//打印出:2。通过iso88591转码,一个汉字的长度是1字节 System.out.println("**********************"); String s_gbk = new String(b_gbk,"GBK"); String s_utf8 = new String(b_utf8,"UTF-8"); String s_iso88591 = new String(b_iso88591,"ISO8859-1"); System.out.println(s_gbk);//打印出:中。 System.out.println(s_utf8);//打印出:中。 System.out.println(s_iso88591);//打印出一个问号"?"。因为无法还原"中"字?原因很简单,因为ISO8859-1编码的编码表根本就不包含汉字字符, }
/* * 有时候,为了让中文字符适应某些特殊要求(如http header要求其内容必须为iso8859-1编码), * 可能会通过将中文字符按照字节方式来编码的情况,如: * String s_iso88591 = new String("中".getBytes("UTF-8"),"ISO8859-1"), * 这样得到的s_iso8859-1字符串实际是三个在ISO8859-1中的字符,在将这些字符传递到目的地后, * 目的地程序再通过相反的方式String s_utf8 = new String(s_iso88591.getBytes("ISO8859-1"),"UTF-8") * 来得到正确的中文汉字"中",这样就既保证了遵守协议规定、也支持中文。 */ private static void method04() throws UnsupportedEncodingException { String s_iso88591 = new String("中".getBytes("UTF-8"),"ISO8859-1");//用utf-8编码,用iso解码 String s_utf8 = new String(s_iso88591.getBytes("ISO8859-1"),"UTF-8");//用iso解码,用utf编码 System.out.println(s_utf8); }
************************************************Array*******************************************************
public static void main(String[] args) { int[] arr = {11,22,44,55,33}; System.out.println(Arrays.toString(arr));//[11, 22, 44, 55, 33] Arrays.sort(arr); System.out.println(Arrays.toString(arr));//[11, 22, 33, 44, 55] }
****************************************StringBuffer*******************************************************
delete
private static void deleteTest() { StringBuffer sb = new StringBuffer("guoxiaofeng"); sb.delete(3, 7); System.out.println(sb.toString());//guofeng sb.deleteCharAt(3); System.out.println(sb.toString());//guoeng //清空缓冲区的方法 System.out.println("清空" + sb.delete(0, sb.length()));//清空 }
insert
private static void insertTest() { StringBuffer sb = new StringBuffer("guoxiaofeng"); sb.insert(3, "hei"); System.out.println(sb.toString());//guoheixiaofeng }
append
/* * 因为全部都在缓冲区,视为同一个 * 下列结果全部打印出: trueheima100 */ private static void appendTest() { StringBuffer sb1 = new StringBuffer(); StringBuffer sb2 = sb1.append(true); StringBuffer sb3 = sb1.append("heima"); StringBuffer sb4 = sb1.append(100); System.out.println(sb1.toString()); System.out.println(sb2.toString()); System.out.println(sb3.toString()); System.out.println(sb4.toString()); }
append
/* * 因为全部都在缓冲区,视为同一个 * 下列结果全部打印出: heima100 */ private static void appendTest() { StringBuffer sb1 = new StringBuffer(); StringBuffer sb2 = sb1.append(true); sb2.delete(0, sb2.length());//清空缓存 StringBuffer sb3 = sb1.append("heima"); StringBuffer sb4 = sb1.append(100); System.out.println(sb1.toString()); System.out.println(sb2.toString()); System.out.println(sb3.toString()); System.out.println(sb4.toString()); }
构造方法
public StringBuffer() { super(16); } public StringBuffer(int capacity) { super(capacity); } public StringBuffer(String str) { super(str.length() + 16); append(str); } public StringBuffer(CharSequence seq) { this(seq.length() + 16); append(seq); }
private static void constructorTest() { StringBuffer sb = new StringBuffer(); System.out.println(sb.length()); //0 System.out.println(sb.capacity());//16 StringBuffer sb2 = new StringBuffer(10); System.out.println(sb2.length()); //0 System.out.println(sb2.capacity());//10 StringBuffer sb3 = new StringBuffer("eric"); System.out.println(sb3.length()); //4 System.out.println(sb3.capacity()); //字符串的长度加上初始容量16。打印出:20 }
****************************************Calendar*******************************************************
Calendar calendar = new GregorianCalendar(); System.out.println(calendar.get(Calendar.YEAR)); //2017 System.out.println(calendar.get(Calendar.MONTH) + 1); //12 System.out.println(calendar.get(Calendar.DAY_OF_MONTH)); //23 System.out.println(calendar.get(Calendar.HOUR_OF_DAY)); //18 System.out.println(calendar.get(Calendar.MINUTE)); //26 System.out.println(calendar.get(Calendar.SECOND)); //13 System.out.println(calendar.get(Calendar.MILLISECOND)); //16
****************************************Collections*******************************************************
Collections.shuffle(List<T> list);//随机输出元素 Collections.reverse(List<T> list); //List元素内反转,因为list内的元素是有序的 Collections.max(List<T> list);//最大值 Collections.min(List<T> list);//最小值 Collections.sort(List<T> list);//排序
Collections.emptyList();
END
END
END