ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-->Arrays类、选择排序、二分查找

2022-05-06 20:02:11  阅读:125  来源: 互联网

标签:arr Java -- System Arrays int println public


 

  • 初识Arrays类的API:
1     public static void main(String[] args) {
2         int[] arr = {5, 4, 6, 8, 0, 1, 3, 2};
3         System.out.println("数组地址:" + arr);
4         System.out.println("数组内容:" + Arrays.toString(arr));
5         Arrays.sort(arr);
6         System.out.println("排序(升序):" + Arrays.toString(arr));
7         System.out.println("二分查找位置:" + Arrays.binarySearch(arr, 2));
8         System.out.println("二分查找位置:" + Arrays.binarySearch(arr, 10));
9     }

示例结果:

 

  •  Comparator接口对应的比较器对应用:
 1 public static void main(String[] args) {
 2         //1、使用API完成数组的升序
 3         int[] ages = {21,22,31,15};
 4         Arrays.sort(ages);
 5         System.out.println(Arrays.toString(ages));
 6         System.out.println("-------API只能升序,如何降序?-------");
 7         //自定义排序(只能支持引用类型):降序
 8         Integer[] ages2 = {12,15,45,30,6};
 9         /**
10          * 参数一 : 被比较的数组
11          * 参数儿 : 匿名内部欸,代表一个比较器对象
12          */
13         Arrays.sort(ages2, new Comparator<Integer>() {
14             @Override
15             public int compare(Integer o1, Integer o2) {
16 //                if(o1 > o2){
17 //                    return 1;
18 //                }
19 //                else if(o1 < o2){
20 //                    return -1;
21 //                }
22 //                else {
23 //                    return 0;
24 //                }
25                 //return o1 - o2;
26                 return -(o1 - o2);
27             }
28         });
29         System.out.println(Arrays.toString(ages2));
30 
31         System.out.println("-----------------");
32         Student[] students = new Student[3];
33         students[0] = new Student("张三",21,1.75);
34         students[1] = new Student("李四",19,1.68);
35         students[2] = new Student("王五",20,1.81);
36         System.out.println(Arrays.toString(students));
37 
38         //Arrays.sort(students)  //会程序崩溃
39         Arrays.sort(students, new Comparator<Student>() {
40             @Override
41             public int compare(Student o1, Student o2) {
42                 //return o1.getAge() - o2.getAge(); //按照年龄升序排序
43                 //return o2.getAge() - o1.getAge(); //按照年龄降序排序
44                 //return Double.compare(o1.getHeight(),o2.getHeight()); //按照身高升序
45                 return Double.compare(o2.getHeight(),o1.getHeight()); //按照升高降序
46             }
47         });
48         System.out.println(Arrays.toString(students));
49     }

Student类:

 1 public class Student {
 2     private String name;
 3     private int age;
 4     private double height;
 5 
 6     public String getName() {
 7         return name;
 8     }
 9 
10     public void setName(String name) {
11         this.name = name;
12     }
13 
14     public int getAge() {
15         return age;
16     }
17 
18     public void setAge(int age) {
19         this.age = age;
20     }
21 
22     public double getHeight() {
23         return height;
24     }
25 
26     public void setHeight(double height) {
27         this.height = height;
28     }
29 
30     public Student() {
31     }
32 
33     public Student(String name, int age, double height) {
34         this.name = name;
35         this.age = age;
36         this.height = height;
37     }
38 
39     @Override
40     public String toString() {
41         return "Student{" +
42                 "name='" + name + '\'' +
43                 ", age=" + age +
44                 ", height=" + height +
45                 '}';
46     }
47 }

示例结果:

 

  •  常见算法(选择排序、二分查找)
  • 选择排序:

 1 public static void main(String[] args) {
 2         int[] arr = {1,5,3,2};
 3         for (int i = 0; i < arr.length - 1; i++) { //外层循环控制轮数
 4             // i = 0  j: 0, 1, 2, 3
 5             // i = 1  j: 1, 2, 3
 6             // i = 2  j: 2, 3
 7             // i = 3  j: 3
 8             //相邻两数选出较小值排在左边
 9             for (int j = i+1; j < arr.length; j++) {
10                 if(arr[i] > arr[j]){
11                     int tmp = arr[i];
12                     arr[i] = arr[j];
13                     arr[j] = tmp;
14                 }
15             }
16         }
17         System.out.println(Arrays.toString(arr));
18     }

示例结果:

 

  •  二分查找:
 1 public static void main(String[] args) {
 2         int[] arr = {1,6,51,98,121,150,194,201,205,255};
 3         int key = 201;
 4         //定义左右边界
 5         int left = 0;
 6         int right = arr.length - 1;
 7         //循环条件
 8         while (left <= right){
 9             int midIndex = (left + right) / 2;
10             //中间元素的值大于所要查找的数据
11             if(arr[midIndex] > key){
12                 right = midIndex - 1;
13             }
14             //中间元素的值小于所要查找的数据
15             else if(arr[midIndex] < key){
16                 left = midIndex + 1;
17             }
18            //中间元素的值等于所要查找的数据
19             else{
20                 System.out.println("找到了,索引为:" + midIndex);
21                 break;
22             }
23         }
24         if(left > right){
25             System.out.println("找不到");
26         }
27     }

标签:arr,Java,--,System,Arrays,int,println,public
来源: https://www.cnblogs.com/yumengqifei/p/16230198.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有