------Java培訓、Android培訓、iOS培訓、.Net培訓、期待與您交流! -------
數組排序和查找
// 選擇排序
public static void selectSort(int[] array){
// i: 0 --> array.length-1
for(int i = 0; i <array.length; i++){
// j: i+1 --> array.length-1
for(int j = i + 1; j <array.length; j++)
// 每一遍都用第一個元素與後面的元素分別比較
if(array[i] >array[j]){
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
// 冒泡排序
public static void bubbleSort(int[] array){
for(int i = 0; i < array.length- 1; i++){
for(int j = 0; j <array.leng - i - 1; j++){
// 相鄰兩個元素比較,大數往後排
if(array[j] >array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
// 冒泡排序優化版
public static void bubbleSortOptimization(int[] array){
boolean flag = false;
for(int i = 0; i < array.length- 1; i++){
for(int j = 0; j <array.length - i - 1; j++){
if(array[j] >array[j+1]){
flag = true;
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
if(!flag){
break;
}
}
}
// 折半查找,返回關鍵字的索引(只適用於有序數組)
public static int binarySearch(int[] array, int key){
int min = 0, max = array.length -1, middle = 0;
while(min <= max){
middle = (min + max) / 2;
if(array[middle] < key){
min = middle + 1;
} else if(array[middle] >key){
max = middle - 1;
} else { // (array[middle] ==key)
return middle;
}
}
return -1;
}
Arrays類 java.util.Arrays
針對數組進行操作的工具類,提供排序、查找等功能
成員方法
public static String toString(int[]a) // 數組 --> 字符串
public static void sort(int[] a)// 對數組進行升序/自然順序排序
public static intbinarySearch(int[] a, int key) // 二分法查找
數組的複製
Arrrays.copyOf
類型[] newArray =Arrays.copyOf(類型[] original, int newLength);
System.arraycopy
System.arraycopy(Objectsrc, int srcPos, Object dest, int destPos, int length)
// 源數組,源數組起始位置,目標數組,目標數組起始位置,要複製長度
數組的擴容
int[] a = {1, 2, 3, 4, 5};
a = Arrays.copyOf(a, a.length + 1)
基本類型包裝類
包裝類是不可變類,在構造了包裝類對象後,不允許更改包裝類在其中的值
包裝類是final的,不能定義他們的子類
用途:
作爲和基本數據類型對應的類類型存在,方便涉及到對象的操作;
包含每種基本數據類型的相關屬性,如最大值、最小值等,以及相關的操作方法
Number及其主要用法
抽象類Number是Byte、Double、Float、Integer、Long和Short類的父類
包裝類 --> 基本類型
byteValue(), shortValue(), int Value(), longValue(), floatValue(), doubleValue()
Integet常用功能
構造方法
public Integer(int value) // int--> Integer
public Integer(String s) // 字符串格式的整數 --> Integer
成員方法
public int intValue() // 以int類型返回該Integer的值
public static int paseInt(Strings) // 字符串 --> int
public static String toString(inti) // 整數 --> 字符串
public static Integer valueOf(inti) // int --> Integer
public static IntegervalueOf(String s) // 字符串--> Integer
Character常用功能
構造方法
public Character(char value)
成員方法
public static booleanisUpperCase(char ch)
public static booleanisLowerCase(char ch)
public static boolean isDigit(charch)
public static chartoUpperCase(char ch)
public static chartoLowerCase(char ch)
自動裝箱和拆箱操作
Integer i = 1; // Integer i =Integer.valueOf(1)
i += 1; // i =Integer.valueOf(i.intValue() + 1);
since JDK5.0 編譯器在編譯期的“預處理”工作,裝箱和拆箱是“編譯器”認可的,而不是JVM。編譯器在生成類的字節碼時插入必要的方法調用。
Integeri = new Integer(10);
Integerj = new Integer(10);
System.out.println(i== j); // false 不同的對象
System.out.println(i.equals(j));// true 重寫equals方法,比較的是數值
Integera = 1000;
Integerb = 1000;
System.out.println(a== b); // false 自動裝箱
System.out.println(a.equals(b));// true
Integeraa = 127;
Integerbb = 127; // Integer bb = aa; 沒有重新開闢存儲空間
// 自動裝箱,取值範圍在 byte 範圍之內,節約資源
System.out.println(aa== bb); // true
System.out.println(aa.equals(bb));// true
如何將int型變量轉換成String類型變量?如何將String類型變量轉換成int類型變量?
int --> String String類型的靜態方法valueOf
String s = String.valueOf(i);
String --> int Integer類型的靜態方法parseInt
int i = Integer.parseInt(s);