【object類】
(1)equals方法:比較兩個對象的內存地址是否相等。
equalis源碼:
public boolean equals(Object obj) {
return (this == obj);
}
重寫equalis源碼:
// 比較Person類型不同對象之間的age是否相等
public boolean equals(Object obj) {
// 若比較的爲同一對象
if(this == obj){
return true;
}
// 排除null
if(obj == null){
return false;
}
// 排除非Person對象
if(obj instanceof Person){
Person p = (Person)obj; // Person爲子類 向下轉型
return (this.age == p.age);
}
else{
return false;
}
}
(2)toString方法
public class Test {
public static void main(String[] args) {
// 調用Person類的方法toString
// 輸出語句中是一個Person對象,默認調用對象的toString方法
Person p = new Person("張三","0001");
System.out.println(p);
System.out.println(p.toString());
}
}
輸出:
usst.javacode.obj.Person@193c0cf
usst.javacode.obj.Person@193c0cf
重寫toString方法
// 重寫toString方法
// 要求:返回類中所有成員變量的值
public String toString(){
return name+Id;
}
public class Test {
public static void main(String[] args) {
// 調用Person類的方法toString
// 輸出語句中是一個Person對象,默認調用對象的toString方法
Person p = new Person("張三","0001");
System.out.println(p);
System.out.println(p.toString());
}
}
輸出:
張三0001
張三0001
【String類】
String類代表 字符串。java程序中所有字符串面值(如"abc")都作此類的實例對象。字符串是常量,它們的值在創建後不能更改。
// str引用變量指向內存變化
// 定義好的字符串對象不變
String str = "秋香";
System.out.println(str); //秋香
str = "春香";
System.out.println(str); //春香
String s1 = new String("abc"); // 創建了兩個對象,"abc"是一個對象,New String()是一個
String s2 = "abc";
System.out.println(s1 == s2); // false 引用數據類型 比較對象的地址
System.out.println(s1.equals(s2)); // true String 重寫了object的equals方法:比較字符串中的每個字符是否相等
【String類的構造函數舉例】
public static void main(String[] args) {
function();
function1();
}
/*
* 定義方法 String類 構造方法
*
* 1.字節數組轉成字符串
* String(byte[] bytes ) 傳遞字節數組
* 使用平臺默認的字符集解碼 指定的byte 數組 構造一個新的String
* 平臺:機器操作系統 默認字符集:操作系統中默認的編碼表 , 默認編碼表GBK
*
* 將字節數組中的每個字節,查詢了編碼表,得到的結果 (字節數爲正,查詢ASCII;爲負查詢GBK)
* 漢字的字節編碼是負數,默認GBK,一個漢字採用2個字節表示
*
* 2.字節數組的一部分轉換成字符串
* String(byte[] bytes, int offset, int length) 字節數組的一部分轉換成字符串
* offset:數組的初始索引 length:數組的個數
*/
public static void function(){
// 1.字節數組轉成字符串
byte[] bytes = {97,98,-99,-100}; // 字節數組
// 調用String類的構造方法,傳遞字節數組
String s = new String(bytes);
System.out.println(s); // ab潨
// 2.字節數組的一部分轉換成字符串
byte[] bytes1 = {65,66,67,68,69};
String s1 = new String(bytes1,1,2);
System.out.println(s1); // BC
}
/*
* 1.傳遞字符數組
* String(char[] value) 將字符數組轉換成字符串,字符數組的參數不查詢編碼表
*
* 2.將字符數組的一部分轉換成字符串
* String(char[] vale, int offset, int count)
* offset:數組開始索引
* count:數組的個數
*
*/
public static void function1(){
// 1.
char[] ch = {'a','b','c','d'};
String s = new String(ch);
System.out.println(s); //"abcd"
// 2.
String s1 = new String(ch,2,1);
System.out.println(s1); // c
}
【String類的方法】
public static void main(String[] args) {
function_8();
}
/*1.返回字符串的長度
* int length()
*/
public static void function_1(){
String str1 = "ababan";
// 調用String類的方法length(),來獲取字符串的長度
int length = str1.length();
System.out.println(length);
}
/*2.獲取字符串的一部分
* String substring(int beginIndex, int endIndex)
* 注意:返回新的字符串
*
* String substring(int beginIndex)
* 獲取指定範圍以後的字符串
*/
public static void function_2(){
String str2 = "pandans";
// 調用String類的方法substring來獲取字符串的一部分
String str3 = str2.substring(1, 3); // [1,3) 包頭不包尾
System.out.println(str3); // an
String str4 = str2.substring(1);
System.out.println(str4); // andans
}
/*3.測試此字符串是否以指定的前綴開始
* boolean startsWith(String prefix)
*
* 判斷一個字符串是否爲另一個字符串的後綴
* boolean endsWith(String prefix)
*/
public static void function_3(){
String str3 = "flower.java";
// 調用String類的方法startsWith,來判斷此字符串是否以指定的前綴開始
boolean s3 = str3.startsWith("flo");
System.out.println(s3); // true
boolean s4 = str3.endsWith(".java");
System.out.println(s4); // true
}
/*4.判斷一個字符串中是否包含另一個字符串
* boolean contains(String s)
*
*/
public static void function_4(){
String str4 = "fofofoffo";
// 調用String類的方法contains,來判斷一個字符串是否包含另一個字符串
boolean s4 = str4.contains("ofo");
System.out.println(s4); // true
}
/*5.返回一個字符在一個字符串中,第一次出現的索引
* int indexof(char ch)
* 若查找的字符不存在,返回-1
*
*/
public static void function_5(){
String str5 = "agshsahshh";
// 調用String 類的indexof 方法,返回一個字符在一個字符串中首次出現的索引
int i5 = str5.indexOf('h');
System.out.println(i5); // 3
}
/*6.將字符串轉換成 字節數組
* byte[] getBytes()
* 此功能與String 構造方法相反,byte數組的相關功能,查詢編碼表
*
*/
public static void function_6(){
String str6 = "當歸";
// 調用String類的方法getBytes,將字符串轉換成字符數組
byte[] bytes6 = str6.getBytes();
for(int i=0;i<bytes6.length;i++){
System.out.println(bytes6[i]); // -75,-79,-71,-23
}
}
/*7.將字符串轉換成字符數組
* char[] toCharArray
* 功能和構造方法相反
*/
public static void function_7(){
String str7 = "abstract";
// 調用String類的方法,將字符串轉換成字符數組
char[] ch7 = str7.toCharArray();
for(int i=0;i<ch7.length;i++){
System.out.print(ch7[i]); // a b s t r a c t
}
}
/*8.判斷字符串中的字符是否完全相同,若完全相等返回true
* boolean equals(object obj)
*
* 判斷字符串中的字符是否相同,忽略大小寫
* boolean equalsIgnoreCase(String s)
*/
public static void function_8(){
String str1 = "Abc";
String str2 = "abc";
// 分別調用equals 和equalsIgnoreCase()
boolean b1 = str1.equals(str2);
boolean b2 = str1.equalsIgnoreCase(str2);
System.out.println(b1); // false
System.out.println(b2); // ture
}
【獲取指定字符串中的大寫,小寫字符,數字的個數】
public class Test1 {
public static void main(String[] args) {
String s = "AAbbngf188";
fun1(s);
System.out.println(toconvert(s));
String str = "AAjavajiijavakkkhjavaj";
String key = "java";
System.out.println(getStringCount(str,key));
}
/* 獲取指定字符串中,大寫字母,小寫字母,數字的個數
* 思想:
* 1.計數器,就是int變量,滿足一個條件就++
* 2.遍歷字符串,長度方法length() + charAt()遍歷
* 3.字符判斷是大寫,小寫數字
*/
public static void fun1(String str){
int upper = 0;
int lower = 0;
int digit = 0;
for(int i=0;i<str.length();i++){
// String類方法charAt,根據索引來獲取字符
char ch = str.charAt(i);
// 編碼表,65~90(A~Z) 97~122(a~z) 48~57(0~9)
if(ch >= 65 && ch <= 90){ // if(ch >= 'A' && ch <= 'Z')
upper++;
}else if(ch >= 97 && ch <= 122){
lower++;
}else if(ch >= 48 && ch <=57){
digit++;
}
}
System.out.println(upper+" "+lower+" "+digit);
}
/*將字符串的首字母改爲大寫,其他元素改爲小寫
* 思想:
* 1.獲取首字母,charAt(0) substring(0,1)
* 2.轉成大寫 toUpperCase()
* 3.其他部分 substring(1) toLowerCase()
*/
public static String toconvert(String str){
// 定義變量,保存首字母 和 剩餘字符
String first = str.substring(0,1);
String after = str.substring(1);
// 調用String類的方法,進行大小寫轉換
first = first.toUpperCase();
after = after.toLowerCase();
return first+after;
}
/*獲取一個字符串中,另一個字符串的個數
* 思路:
* 1.indexof 從一個字符串中找另一個字符串第一次出現的索引
* 2.找到索引+所找字符串的長度,截取字符串 substring()
* 3.count計數
*/
public static int getStringCount(String str, String key){
// 定義計數器
int count = 0;
// 定義變量,保存indexof查找後的索引
int index = 0;
while( (index = str.indexOf(key)) != -1 ){
count++;
str = str.substring(index+key.length());
}
return count;
}
}
【StringBuffer】
/*
* StringBuffer 字符串緩衝區 提高了字符串的操作效率
* 內部採用了可變數組的方法實現,類內部定義類數組,這個數組沒有final
* char[]數組 默認容量爲16個字符
*
*/
public class Test2 {
public static void main(String[] args) {
function_1();
}
/* 1.StringBuffer類方法
* StringBuffer append,將任意類型的數據,添加到緩衝區
*
* 2.delete(int star, int end) 刪除緩衝區內容 注意:包頭不包尾
*
* 3.insert(int index, 任意類型) 將任意類型的數據,插入到緩衝區指定的索引上去
*
* 4.replace(int star, int end, String str) 將指定索引範圍內的字符,替換爲新的字符串
*
* 5.reverse() 將緩衝區中的字符翻轉
*
* 6.String toString() 繼承object,重寫toString() 將緩衝區內的所有字符轉換成 字符串
*/
public static void function_1(){
StringBuffer buffer = new StringBuffer();
// 1.調用StringBuffer方法append向緩衝區追加內容
buffer.append(61234).append(true);
System.out.println(buffer); // 6true
// 2.調用StringBuffer方法delete,刪除緩衝區內容
buffer.delete(1,3);
System.out.println(buffer); // 634true
// 3.調用StringBuffer方法insert,將任意類型的數據,插入到緩衝區指定的索引上
buffer.insert(0,19.01);
System.out.println(buffer); //19.01634true
// 4.調用StringBuffer方法replace,將指定範圍內的字符,替換爲新的字符串
buffer.replace(0, 2, "zp");
System.out.println(buffer); // zp.01634true
// 5.調用StringBuffer方法reverse,將字符串中的字符翻轉
buffer.reverse();
System.out.println(buffer); // eurt43610.pz
// 6.調用StringBuffer方法toString,將緩衝區中的字符轉換成字符串
buffer.append(1212);
// 將可變的緩衝區對象,變成不可變的String對象
String s = buffer.toString();
System.out.println(s); //eurt43610.pz1212
}
}
public class Test3 {
public static void main(String[] args) {
int[] arr = {12,34,55,26};
toString(arr);
}
/* 將數組 {12,34,55,26},轉換成[12,34,55,26]的形式
* 使用StringBuffer,節約內存空間
*/
public static void toString(int[] arr){
// 創建字符緩衝區
StringBuffer buffer = new StringBuffer();
buffer.append("[");
// 數組遍歷
for(int i=0;i<arr.length;i++){
if(i == arr.length-1){
buffer.append(arr[i]).append("]");
}else{
buffer.append(arr[i]).append(",");
}
}
System.out.println(buffer); // [12,34,55,26]
}
}