數組、字符串、集合
數組與集合的轉換、數組與字符串的轉換
========數組變集合
String[] arr = {"abc","cc","kkkk"};
//把數組變成list集合有什麽好處?
/*
可以使用集合的思想和方法來操作數組中的元素。
註意:將數組變成集合,不可以使用集合的增刪方法。
因為數組的長度是固定。
contains。
get
indexOf()
subList();
如果你增刪。那麽會產生UnsupportedOperationException,
*/
List<String> list = Arrays.asList(arr);
//sop("contains:"+list.contains("cc"));
//list.add("qq");//UnsupportedOperationException,
//sop(list);
//int[] nums = {2,4,5};
Integer[] nums = {2,4,5};
List<Integer> li = Arrays.asList(nums);
/*
如果數組中的元素都是對象。那麽變成集合時,數組中的元素就直接轉成集合中的元素。
如果數組中的元素都是基本數據類型,那麽會將該數組作為集合中的元素存在。
*/
=======集合變數組
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add("abc1");
al.add("abc2");
al.add("abc3");
/*
1,指定類型的數組到底要定義多長呢?
當指定類型的數組長度小於了集合的size,那麽該方法內部會創建一個新的數組。長度為集合的size。
當指定類型的數組長度大於了集合的size,就不會新創建了數組。而是使用傳遞進來的數組。
所以創建一個剛剛好的數組最優。
2,為什麽要將集合變數組?
為了限定對元素的操作。不需要進行增刪了。
*/
String[] arr = al.toArray(new String[al.size()]);
System.out.println(Arrays.toString(arr));
}
========字符數組轉換為字符串
String str = new String(char[]);
========字符串轉換成字符數組
char[] chs = str.roCharArray();
========字節數組轉換為字符串
String s = new String(byte[]);
========字符串轉換成字節數組
byte b = s.getBytes();
String相關總結
String
字符串是常量;它們的值在創建之後不能更改。字符串緩沖區支持可變的字符串。因為 String 對象是不可變的,所以可以共享。
String類為什麽是final
主要是為了“效率” 和 “安全性” 的緣故。若 String允許被繼承, 由於它的高度被使用率, 可能會降低程序的性能,所以String被定義成final。
所有字符串不能被修改。
一.string基本概念
public class TestString { public static void test1(){ String aa = "aa"; System.out.println("aa=" + aa.hashCode()); String bb = "aa"; System.out.println("bb=" + bb.hashCode()); if(aa == bb){ System.out.println("aa==bb"); }else{ System.out.println("aa!=bb"); } if(aa.equals(bb)){ System.out.println("aa內容==bb"); } String cc = "a" + "b" + "c"; //可能有五塊內存:”a”\”b”\”c”\”ab”\”abc” } public static void main(String[] args) { TestString.test1(); } } |
執行結果: aa=3104 bb=3104 aa==bb aa內容==bb
|
public static void test2() { String s1 = "abc"; String s2 = new String("abc"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); }
public static void main(String[] args) { TestString.test2(); } |
false true |
1,獲取。
1.1 字符串中的包含的字符數,也就是字符串的長度。
int length():獲取長度。
1.2 根據位置獲取位置上某個字符。
char charAt(int index):
1.3 根據字符獲取該字符在字符串中位置。
int indexOf(int ch):返回的是ch在字符串中第一次出現的位置。
int indexOf(int ch, int fromIndex) :從fromIndex指定位置開始,獲取ch在字符串中出現的位置。
int indexOf(String str):返回的是str在字符串中第一次出現的位置。
int indexOf(String str, int fromIndex) :從fromIndex指定位置開始,獲取str在字符串中出現的位置。
int lastIndexOf(int ch) :
2,判斷。
2.1 字符串中是否包含某一個子串。
boolean contains(str):
特殊之處:indexOf(str):可以索引str第一次出現位置,如果返回-1.表示該str不在字符串中存在。
所以,也可以用於對指定判斷是否包含。
if(str.indexOf("aa")!=-1)
而且該方法即可以判斷,有可以獲取出現的位置。
2.2 字符中是否有內容。
boolean isEmpty(): 原理就是判斷長度是否為0.
2.3 字符串是否是以指定內容開頭。
boolean startsWith(str);
2.4 字符串是否是以指定內容結尾。
boolean endsWith(str);
2.5 判斷字符串內容是否相同。復寫了Object類中的equals方法。
boolean equals(str);
2.6 判斷內容是否相同,並忽略大小寫。
boolean equalsIgnoreCase();
3,轉換。
3.1 將字符數組轉成字符串。
構造函數:String(char[])
String(char[],offset,count):將字符數組中的一部分轉成字符串。
靜態方法:
static String copyValueOf(char[]);
static String copyValueOf(char[] data, int offset, int count)
static String valueOf(char[]):
3.2 將字符串轉成字符數組。**
char[] toCharArray():
3.3 將字節數組轉成字符串。
String(byte[])
String(byte[],offset,count):將字節數組中的一部分轉成字符串。
3.4 將字符串轉成字節數組。
byte[] getBytes():
3.5 將基本數據類型轉成字符串。
static String valueOf(int)
static String valueOf(double)
//3+"";//String.valueOf(3);
特殊:字符串和字節數組在轉換過程中,是可以指定編碼表的。
4,替換
String replace(oldchar,newchar);
5,切割
String[] split(regex);
6,子串。獲取字符串中的一部分。
String substring(begin);
String substring(begin,end);
7,轉換,去除空格,比較。
7.1 將字符串轉成大寫或則小寫。
String toUpperCase();
String toLowerCase();
7.2 將字符串兩端的多個空格去除。
String trim();
7.3 對兩個字符串進行自然順序的比較。
int compareTo(string);
二.StringBuffer是字符串緩沖區。
特點:
1,長度是可變化的。
2,可以字節操作多個數據類型。
3,最終會通過toString方法變成字符串
C create U update R read D delete
1,存儲。
StringBuffer append():將指定數據作為參數添加到已有數據結尾處。
StringBuffer insert(index,數據):可以將數據插入到指定index位置。
2,刪除。
StringBuffer delete(start,end):刪除緩沖區中的數據,包含start,不包含end。
StringBuffer deleteCharAt(index):刪除指定位置的字符。
3,獲取。
char charAt(int index)
int indexOf(String str)
int lastIndexOf(String str)
int length()
String substring(int start, int end)
4,修改。
StringBuffer replace(start,end,string);
void setCharAt(int index, char ch) ;
5,反轉。
StringBuffer reverse();
6,
將緩沖區中指定數據存儲到指定字符數組中。
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
JDK1.5 版本之後出現了StringBuilder.都一樣只不過把其換了就行
StringBuffer是線程同步。
StringBuilder是線程不同步。
以後開發,建議使用StringBuilder
三.基本數據類型對象包裝類。
Byte Byte
shortshort
intInteger
long Long
boolean Boolean
float Float
doubleDouble
char Character
基本數據類型對象包裝類的最常見作用,
就是用於基本數據類型和字符串類型之間做轉換
基本數據類型轉成字符串。
基本數據類型+""
基本數據類型.toString(基本數據類型值);
如: Integer.toString(34);//將34整數變成"34";
字符串轉成基本數據類型。
xxx a = Xxx.parseXxx(String);
int a = Integer.parseInt("123");
double b = Double.parseDouble("12.23");
boolean b = Boolean.parseBoolean("true");
Integer i = new Integer("123");
int num = i.intValue();
十進制轉成其他進制。
toBinaryString();
toHexString();
toOctalString();
其他進制轉成十進制。
parseInt(string,radix);
本文出自 “秦斌的博客” 博客,請務必保留此出處http://qinbin.blog.51cto.com/11773640/1967013
數組、字符串、集合