15Arrays工具類
A:Arrays工具類:
public class ArraysDemo {
public static void main(String[] args) {
function_2();
int[] arr = {56,65,11,98,57,43,16,18,100,200};
int[] newArray = test(arr);
System.out.println(Arrays.toString(newArray));
}
/*
* 定義方法,接收輸入,存儲的是10個人考試成績
* 將最后三個人的成績,存儲到新的數(shù)組中,返回新的數(shù)組
*/
public static int[] test(int[] arr){
//對數(shù)組排序
Arrays.sort(arr);
//將最后三個成績存儲到新的數(shù)組中
int[] result = new int[3];
//成績數(shù)組的最后三個元素,復制到新數(shù)組中
// System.arraycopy(arr, 0, result, 0, 3);
for(int i = 0 ; i < 3 ;i++){
result[i] = arr[i];
}
return result;
}
/*
* static String toString(數(shù)組)
* 將數(shù)組變成字符串
*/
public static void function_2(){
int[] arr = {5,1,4,6,8,9,0};
String s = Arrays.toString(arr);
System.out.println(s);
}
/*
* static int binarySearch(數(shù)組, 被查找的元素)
* 數(shù)組的二分搜索法
* 返回元素在數(shù)組中出現(xiàn)的索引
* 元素不存在, 返回的是 (-插入點-1)
*/
public static void function_1(){
int[] arr = {1,4,7,9,11,15,18};
int index = Arrays.binarySearch(arr, 10);
System.out.println(index);
}
/*
* static void sort(數(shù)組)
* 對數(shù)組升序排列
*/
public static void function(){
int[] arr = {5,1,4,6,8,9,0};
Arrays.sort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
16數(shù)組復制練習
A:數(shù)組復制練習:
public static void main(String[] args) {
int[] arr = {56,65,11,98,57,43,16,18,100,200};
int[] newArray = test(arr);
System.out.println(Arrays.toString(newArray));
}
/
* 定義方法,接收輸入,存儲的是10個人考試成績
* 將最后三個人的成績,存儲到新的數(shù)組中,返回新的數(shù)組
*/
public static int[] test(int[] arr){
//對數(shù)組排序
Arrays.sort(arr);
//將最后三個成績存儲到新的數(shù)組中
int[] result = new int[3];
//成績數(shù)組的最后三個元素,復制到新數(shù)組中
//System.arraycopy(arr, 0, result, 0, 3);
for(int i = 0 ; i < 3 ;i++){
result[i] = arr[i];
}
return result;
}