[Java 教學範例拷貝]- Arrays 類別(1/4)
[Java 教學範例拷貝]- Arrays 類別(1/4)
剛才找資料時發現一個的Java 教學網站,趕快發揮(C/P)的長才將它備份來,有需要的同好,歡迎來(C/P)一下^^。
拷貝來源:
http://openhome.cc/Gossip/JavaGossip-V1/
http://openhome.cc/Gossip/JavaGossip-V1/ArraysClass.htm
import java.util.Scanner;
import java.util.Arrays;
public class UseArrays {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] arr = {93, 5, 3, 55, 57, 7, 2 ,73, 41, 91};
System.out.print("排序前: ");
for(int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.println();
Arrays.sort(arr);
System.out.print("排序後: ");
for(int i = 0; i < arr.length; i++)
System.out.print(arr[i] + " ");
System.out.print("\n請輸入搜尋值: ");
int key = scanner.nextInt();
int find = -1;
if((find = Arrays.binarySearch(arr, key)) > -1) {
System.out.println("找到值於索引 " +
find + " 位置");
}
else
System.out.println("找不到指定值");
}
}
|