java基础-Arrays类常用方法介绍
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.Array类的概念
此类包含用来操作数组(比如排序和搜索)的各种方法。需要注意,如果指定数组引用为 null,则访问此类中的方法都会抛出空指针异常NullPointerException。
二.Arrays常用方法
1>.对数组升序排列(public static void sort(数组) )
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.Arrays; 10 11 public class MathDemo { 12 public static void main(String[] args) { 13 int[] arr = {9,5,2,7,2,0,1,8}; 14 System.out.print("排序前:"); 15 printArray(arr); 16 Arrays.sort(arr); 17 System.out.print("排序后:"); 18 printArray(arr); 19 } 20 21 public static void printArray(int[] arr) { 22 for (int i = 0; i < arr.length; i++) { 23 System.out.print(arr[i]+" "); 24 } 25 System.out.println(); 26 } 27 28 } 29 30 31 /* 32 以上代码执行结果如下: 33 排序前:9 5 2 7 2 0 1 8 34 排序后:0 1 2 2 5 7 8 9 35 */
2>.数组的二分搜索法(public static int binarySearch(数组,被查找的元素))
返回元素在数组中出现的索引,如果元素不存在,则返回的是(-插入点-1),也就是说它会根据你需要查找的元素应该存在的位置索引,将该索引值再减去1即可。
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.Arrays; 10 11 public class MathDemo { 12 public static void main(String[] args) { 13 int[] arr = {0,1,2,3,5,7,8,9,15,18,21}; 14 int index = Arrays.binarySearch(arr, 10); 15 System.out.println(index); 16 index = Arrays.binarySearch(arr, 15); 17 System.out.println(index); 18 } 19 } 20 21 22 /* 23 以上代码执行结果如下: 24 -9 25 8 26 */
3>.将数组变成字符串(public static String toString(数组))
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.Arrays; 10 11 public class MathDemo { 12 public static void main(String[] args) { 13 int[] arr = {0,1,2,3,5,7,8,9,15,18,21}; 14 String s = Arrays.toString(arr); 15 System.out.println(s); 16 } 17 } 18 19 20 /* 21 以上代码执行结果如下: 22 [0, 1, 2, 3, 5, 7, 8, 9, 15, 18, 21] 23 */
三.小试牛刀
定义一个方法,接收一个数组,数组中存储10个学生考试分数,考方法要求返回考试分数最低的后三名考试分数。
1 /* 2 @author :yinzhengjie 3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/ 4 EMAIL:y1053419035@qq.com 5 */ 6 7 package cn.org.yinzhengjie.demo; 8 9 import java.util.Arrays; 10 import java.util.Scanner; 11 12 public class ArrayDemo { 13 public static void main(String[] args) { 14 int[] arr = new int[10]; 15 Scanner s = new Scanner(System.in); 16 int index = 0; 17 boolean flag = true; 18 while(flag) { 19 System.out.printf("请输入第%d学生的分数:",index+1); 20 String Input = s.next(); 21 try { 22 int score = Integer.parseInt(Input); 23 24 if(index ==9 ) { 25 flag = false; 26 } 27 arr[index++] = score; 28 }catch(NumberFormatException e) { 29 System.out.println("传入的数据有误!"); 30 System.out.println(index); 31 } 32 } 33 int[] newArray = getTheLastThreeNumber(arr); 34 System.out.println("最后三名同学的成绩是:"+Arrays.toString(newArray)); 35 } 36 37 public static int[] getTheLastThreeNumber(int[] arr) { 38 //1>.对数组进行排序 39 Arrays.sort(arr); 40 //2>.将最后三个成绩存储到新的数组中 41 int[] result = new int[3]; 42 System.arraycopy(arr, 0, result, 0, 3); 43 return result; 44 } 45 }