Arrays針對數組進行操作的工具類

package niu.cheng3;


import java.util.Arrays;
/* 
 * Arrays針對數組進行操作的工具類(排序,查找,....)
 * 
 * public static String toString(int[] a)//把數組轉換成字符串
 * public static void sort(int[] a)//把數組進行排序
 * public static int binarySearch(int[] a,int key)使用二分搜索法來搜索指定的 int 型數組,以獲得指定的值。
 */
public class ArraysDemo {
public static void main(String[] args) {
int[] x={7,6,3,4,8,2,1};
//public static String toString(int[] a)//把數組轉換成字符串
System.out.println("排序前:"+Arrays.toString(x));//排序前:[7, 6, 3, 4, 8, 2, 1]
System.out.println("--------");

//public static void sort(int[] a)//把數組進行排序
Arrays.sort(x);
System.out.println("排序後:"+Arrays.toString(x));//排序後:[1, 2, 3, 4, 6, 7, 8]
System.out.println("--------");


//這個是在排序之後查找的,不建議使用二分查找
//public static int binarySearch(int[] a,int key)使用二分搜索法來搜索指定的 int 型數組,以獲得指定的值。
//System.out.println("binarySearch:"+Arrays.binarySearch(x, 7)); //binarySearch:5

//public static int binarySearch(int[] a,int key)使用二分搜索法來搜索指定的 int 型數組,以獲得指定的值。
int [] y={11,22,33,44,55,66,77,88,99};
System.out.println("binarySearch:"+Arrays.binarySearch(y, 55));//binarySearch:4
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章