1.數組foreach 循環
JDK 1.5 引進了一種新的循環類型,被稱爲 foreach 循環或者加強型循環,它能在不使用下標的情況下遍歷數組。
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// 打印所有數組元素
for (double element: myList) {
System.out.println(element);
}
}
}
注意:foreach不能代替for語句,因爲foreach不能修改數組中內容。
2.Arrays 類
- 給數組賦值:通過 fill 方法。
- 對數組排序:通過 sort 方法,按升序。
- 比較數組:通過 equals 方法比較數組中元素值是否相等。
- 查找數組元素:通過 binarySearch 方法能對排序好的數組進行二分查找法操作。
- public static int binarySearch(Object[] a, Object key)
用二分查找算法在給定數組中搜索給定值的對象(Byte,Int,double等)。數組在調用前必須排序好的。如果查找值包含在數組中,則返回搜索鍵的索引;否則返回 (-(插入點) - 1)。 - public static boolean equals(long[] a, long[] a2)
如果兩個指定的 long 型數組彼此相等,則返回 true。如果兩個數組包含相同數量的元素,並且兩個數組中的所有相應元素對都是相等的,則認爲這兩個數組是相等的。換句話說,如果兩個數組以相同順序包含相同的元素,則兩個數組是相等的。同樣的方法適用於所有的其他基本數據類型(Byte,short,Int等)。 - public static void fill(int[] a, int val)
public static void fill(int[] a, int index, int end, int val)
將指定的 int 值分配給指定 int 型數組指定範圍中的每個元素。同樣的方法適用於所有的其他基本數據類型(Byte,short,Int等)。 - public static void sort(Object[] a)
public static void fill(int[] a, int index, int end, int val)
對指定對象數組根據其元素的自然順序進行升序排列。同樣的方法適用於所有的其他基本數據類型(Byte,short,Int等)。
3.Java Character 類【字符】
3.1 實例
char ch = 'a';
// Unicode 字符表示形式
char uniChar = '\u039A';
// 字符數組
char[] charArray ={ 'a', 'b', 'c', 'd', 'e' };
// 使用Character的構造方法創建一個Character類對象
Character ch = new Character('a');
// 自動裝箱
Character ch = 'a';
3.2 轉義序列
\t 在文中該處插入一個tab鍵
\b 在文中該處插入一個後退鍵
\n 在文中該處換行
\r 在文中該處插入回車
\f 在文中該處插入換頁符
’ 在文中該處插入單引號
" 在文中該處插入雙引號
\\ 在文中該處插入反斜槓
注意:
\r: return 到當前行的最左邊。
\n: newline 向下移動一行,並不移動左右。
Linux中\n表示:回車+換行;
Windows中\r\n表示:回車+換行。
Mac中\r表示:回車+換行。
3.3 Character 方法【重點】
| 序號 | 方法 |
| ------------- |: -----|
| 1 | isLetter()是否是一個字母 |
| 2 | isDigit()是否是一個數字字符 |
| 3 | isWhitespace()是否是一個空格 |
| 3 | isUpperCase() 是否是大寫字母 |
| 3 | isLowerCase() 是否是小寫字母 |
| 3 | toUpperCase() 指定字母的大寫形式 |
| 3 | toLowerCase()指定字母的小寫形式 |
| 3 | toString() 返回字符的字符串形式,字符串的長度僅爲1 |
4.Java String 類
String 類是不可改變的,所以你一旦創建了 String 對象,那它的值就無法改變了
如果需要對字符串做很多修改,那麼應該選擇使用 StringBuffer & StringBuilder 類。
4.1 連接字符串
//offset - 要解碼的第一個 byte 的索引
//length - 要解碼的 byte 數
string1.concat(string2);
"Hello," + " runoob" + "!"
//charsetName - 受支持 charset 的名稱
String(byte[] bytes, int offset, int length, Charset charset) ;
String(int[] codePoints, int offset, int count);
String(StringBuffer buffer);
String(StringBuilder builder);
4.2 String 方法
-
char charAt(int index)【重要,指定位置的字符】
-
int indexOf(int ch)【重要,指定字符出現位置】
int indexOf(int ch, int fromIndex)
int indexOf(String str)
int indexOf(String str, int fromIndex) -
char[] toCharArray()【重要,轉成char[]】
將此字符串轉換爲一個新的字符數組。 -
static String **copyValueOf**(char[] data)【重要,char[]轉String】
static String copyValueOf(char[] data, int offset, int count)
offset – 子數組的初始偏移量。。
count – 子數組的長度。
-
boolean **matches**(String regex)【重要,正則匹配】
告知此字符串是否匹配給定的正則表達式。
-
String replace(char oldChar, char newChar)【重要,新字符替換指定舊字符】
String replaceAll(String regex, String replacement【重要,替換匹配正則對舊字符】
使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。 -
String[] split(String regex)【重要,根據正則分割】
String[] split(String regex, int limit)
limit是拆成幾份;
根據給定正則表達式的匹配拆分此字符串。 -
String substring(int beginIndex, int endIndex)【重要,返回子字符串】
返回一個新字符串,它是此字符串的一個子字符串。
beginIndex – 起始索引(包括)。
endIndex – 結束索引(不包括)。 -
String **trim**()【實用,去前後空格】
返回字符串的副本,忽略前導空白和尾部空白。
-
static String valueOf(primitive data type x)【實用,data類型轉String】
返回給定data type類型x參數的字符串表示形式。 -
int compareTo(String anotherString)
int compareToIgnoreCase(String str)
按字典順序比較兩個字符串。計算這兩個不同字符的ASCII碼的差,作爲返回值。(第二個不考慮大小寫。)
如果參數字符串等於此字符串,則返回值 0;
如果此字符串小於字符串參數,則返回一個小於 0 的值;
如果此字符串大於字符串參數,則返回一個大於 0 的值。 -
boolean equals(Object anObject)
boolean equalsIgnoreCase(String anotherString)
如果給定對象與字符串相等,則返回 true;否則返回 false。
返回指定數組中表示該字符序列的 String。 -
boolean endsWith(String suffix)
測試此字符串是否以指定的後綴結束。 -
byte[] getBytes()
byte[] getBytes(String charsetName)
使用指定的字符集將此 String 編碼爲 byte 序列,並將結果存儲到一個新的 byte 數組中。 -
int hashCode()
返回此字符串的哈希碼。 -
int lastIndexOf(int ch)
int lastIndexOf(int ch, int fromIndex)
int lastIndexOf(String str)
int lastIndexOf(String str, int fromIndex)
返回指定字符在此字符串中最後一次出現處的索引,從指定的索引處開始進行反向搜索。 -
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
測試兩個字符串區域是否相等。
ignoreCase – 如果爲 true,則比較字符時忽略大小寫。
toffset – 此字符串中子區域的起始偏移量。
other – 字符串參數。
ooffset – 字符串參數中子區域的起始偏移量。
len – 要比較的字符數。 -
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
將字符從此字符串複製到目標字符數組。
srcBegin – 字符串中要複製的第一個字符的索引。
srcEnd – 字符串中要複製的最後一個字符之後的索引。
dst – 目標數組。
dstBegin – 目標數組中的起始偏移量。 -
String toUpperCase()
String toLowerCase()
轉大小寫