1.(補充前面課程)
關於代碼中使用到其他的class中的某個方法時,只要保證調用的.class文件存在於當前的classpath路徑中!!
如: 在Exercise1.java中使用了Exercise2.print()方法,
2.排序:
a.選擇排序:selectSort()
//選擇排序
static void selectSort(int [] arr) {
for(int i=0;i<arr.length-1;i++) {
for(int j=i+1;j<arr.length;j++) {
if(arr[i]>arr[j])
swap(arr,i,j);
}
}
}
b.冒泡排序:bubbleSort()
//冒泡排序
static void bubbleSort(int [] arr) {
for(int i=0;i<arr.length-1;i++) {
for(int j=0;j<arr.length-1-i;j++) {
if(arr[j]>arr[j+1])
swap(arr,j,j+1);
}
}
}
3.API的類:
a.Arrays類:
Arrays.sort()
Arrays.binarySearch()
Array.toString()
b.System類:
System.arraycopy()
c.Integer類:
Integer.parseInt(String s)
Integer.toHexString()
Integer.toBinaryString()
4.二分查找:
(前提: 數組數據已經有序並且沒有重複元素!!)
static int binarySearch(int[] arr,int key) {
int start = 0;
int end = arr.length-1;
int middle = (start+end)/2;
while(start<=end) {
if(key>arr[middle])
start=middle+1;
else if(key < arr[middle])
end=middle-1;
else
return middle;
middle=(start+end)/2;
}
return -1;
}
5.多維數組:
多維數組的四種定義方式:
int[][] arr={{1,2},{3,4,5},{5,8}};
int[][] arr=new int[][]{{1,2},{3,4,5},{5,8}};
int[][] arr=new int[2][4];
int[][] arr=new int[2][];
arr[0]= new int[1];
arr[1]= new int[3];