當前位置:
首頁 > 知識 > 包裝類、數組、string類淺析及練習

包裝類、數組、string類淺析及練習

String s1 = "abc";
String s2 = "abc";
System.out.println(s1==s2); //返回true string s1=『abc』 string s2=『abc』 string的值是存儲於常量池裡邊的,當給string賦值的時候回先到常量池尋找是否有相同的值。

String s3 = new String("abc");
String s4 = new String("abc");
System.out.println(s3==s4); //返回false

System.out.println(s3.equals(s4))//返回true

基礎類型數據存儲於棧空間 引用類型數據存儲於堆空間

equals方法: String里的equals方法重寫了object對象里的equals方法

String里的equals方法第一步也是會判斷地址值是否相等,相等返回true ,不相等再去判斷裡面的值是否相等。

str.length 返回整個字元串的長度。

str.trim 去掉字元串兩邊的空格

str.trim.length 返回整個字元串的長度

str.charAt; 取出字元串中指定索引位置的字元

str.contains(CharSequence s); 當此字元串包含指定的 char 值時,返回 true。

str.startWith(String s); 判斷字元串是否以括弧中的字元串開始

str.endWith(String s); 判斷字元串是否以括弧中的字元串結束

replace(char o, char n); 將指定的字元替換掉指定的字元

replace(CharSequence o, CharSequence n); 將指定的字元串替換掉指定的字元串

split(String s); 將字元串分割成字元串

toUpperCase; 將字元串轉換成大寫

toLowerCase; 將字元串轉換成小寫

valueOf(any args);

str.indexOf(String s); 返回指定子字元串在此字元串中第一次出現處的索引。

str.lastIndexOf(String s);返回指定子字元串在此字元串中最後一次出現處的索引。

str.substring(int i); 返回一個新的字元串,它是此字元串的一個子字元串

str.substring(int a, int b); str.substring( int beginIndex, int endIndex)

package com.test;

public class lianxi {
public static void main(String[] args) {
String str = "像勇士這樣的球隊,只有防守一鬆懈,他們才能抓住機會,"
+ "打完了三場,爵士還是沒找到應對勇士的辦法";
//"球隊","機會"
System.out.println(str.indexOf("球隊"));
System.out.println(str.indexOf("機會"));
System.out.println(str.lastIndexOf("勇士"));

int m = str.indexOf("球隊") + str.indexOf("機會") + str.lastIndexOf("勇士");
System.out.println(m);
System.out.println((char)m);
System.out.println(str.split(",")[4]);

String newstr = str.split(""); //用空字元串將這一段話分割成多個以每一個字為一個字元串的數組。
String temp = "";
for (int i = 0; i < newstr.length; i++) { if(newstr[i].equals("勇")) { newstr[i] = "爵"; } else if(newstr[i].equals("爵")) { newstr[i] = "勇"; } temp+=newstr[i]; } System.out.println(temp); //勇士抓住機會,找到應對辦法 System.out.print(str.substring(str.indexOf("勇士"),str.indexOf("勇士")+2)); System.out.print(str.substring(str.indexOf("抓住機會"),str.indexOf("抓住機會")+4)); System.out.print(str.substring(str.indexOf(","),str.indexOf(",")+1)); System.out.print(str.substring(str.indexOf("找到應對"),str.indexOf("找到應對")+4)); System.out.print(str.substring(str.indexOf("辦法"),str.indexOf("辦法")+2)); System.out.println; String qqEmail = "123@qq.com"; System.out.println(qqEmail.substring(0,qqEmail.indexOf("@")));

//增強for循環
public class ForArray
{
public static void main(String[] args)
{
int arr = {1,2,3,4,5,6,7};

for(int a:arr)
{
System.out.print(a);
}
}
}
/*
for (循環變數類型 循環變數名稱 : 要被遍歷的對象) 循環體
例子中,
1.arr就是為 要被遍歷的對象,可以是數組,集合
2.循環變數類型就是這個arr數組的數據類型,即int
3.循環變數名稱就是a,這個可以自己定義
*/

包裝類:

Integer.parseInt;

byte---Byte

short---Short

int---Integer

long---Long

float---Floatdouble---Double

boolean---Boolean

char---Character

System.out.println(Integer.MAX_VALUE);

System.out.println(Byte.MIN_VALUE);

System.out.println(Byte.MAX_VALUE);

System.out.println(Long.MIN_VALUE);

System.out.println(Long.MAX_VALUE);

System.out.println(Short.MIN_VALUE);

System.out.println(Short.MAX_VALUE);

System.out.println(Float.MIN_VALUE);

System.out.println(Float.MAX_VALUE);

System.out.println(Double.MIN_VALUE);

System.out.println(Double.MAX_VALUE);

//空心正方形(進入for循環先判斷i的值,如果i的值在1~7之間而且j=0或者7的時候,那麼列印出除了第一行與最後一行的※,當i=1或7的時候第一行跟最後一行的※都列印出來)
/*for( int i=0; i<8; i++){ if(i>0&&i<7){ //先判斷 i的值是否在1-7這個區間, for(int j=0;j<8;j++){ if(j==0||j==7){ System.out.print("*"+" ");} else{ System.out.print(" "+" ");} } }else{ for( int l=0; l<8;l++){ System.out.print("*"+" "); } }System.out.println; }*/ //輸出空心菱形。 for( int i=4; i<7; i++){ for( int j=8;j>0;j--){
if(j==i||j==8-i){
System.out.print("*");
}else{
System.out.print(" ");
}
}System.out.println;
}
for( int i=1; i<5; i++){ for( int j=8;j>0;j--){
if(j==i||j==8-i){
System.out.print("*");
}else{
System.out.print(" ");
}
}System.out.println;
}

//實心菱形
for( int i=4; i>0; i--){ //菱形的上半部分是隨著i值得變動輸出的*越來越多,所以i值應該是自減
for( int j=0;j<8;j++){ if(j>i&&j<8-i){ System.out.print("*"); }else{ System.out.print(" "); } }System.out.println; } for( int i=0; i<4; i++){ for( int j=0;j<8;j++){ if(j>i&&j<8-i){ System.out.print("*"); }else{ System.out.print(" "); } }System.out.println; } }

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

Hadoop源碼系列(一)FairScheduler申請和分配container的過程
Android系統——輸入系統(十五)實戰 使用GlobalKey一鍵啟動程序
MySql單表最大8000W+ 之資料庫遇瓶頸記
一顆簡單的JDBC栗子
nodejs伺服器部署教程二

TAG:達人科技 |

您可能感興趣

淺析 AnyCast 技術
淺析Sentinel Protocol:區塊鏈資產的守護者
【Listing】淺析Listing的整體排布思路
淺析Wker_Xsser使用
淺析requests庫響應對象的text和content屬性
Oracle 資料庫中enq:TX-index contention等待時間淺析
MapReduce數據序列化讀寫概念淺析!
淺析Windows7 64位旗艦版系統庫的功能和使用技巧
一文淺析NVMe over Fabric技術發展簡史
乾貨分享!CynosDB for PostgreSQL 架構淺析
推薦 淺析兩代MobileNet
淺析Myobrace的結構與功能作用的關係
深入淺析一致性模型之Linearizability
Ubuntu 下面 MySQL 的參數文件 my.cnf 淺析
淺析Jacklove、iBoy、Smlz,誰成為下一個Uzi
淺析minecraft我的世界
RabbitMq運行原理淺析
ChinaJoy是什麼?淺析:ChinaJoy的歷史
SQL Server中LIKE %search_string% 索引查找(Index Seek)淺析
Android AIDL淺析及非同步使用