Arrays(java)
【摘要】
package com.arrays;
import java.util.Arrays;
public class Test {
public static void main(String[] ...
package com.arrays;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int [] arr={11,22,44,88,66,33,55,77};
//返回地址
System.out.println(arr); //[I@1b6d3586
//返回数组的内容
System.out.println(Arrays.toString(arr)); //[11, 22, 44, 88, 66, 33, 55, 77]
//排序。升序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); //[11, 22, 33, 44, 55, 66, 77, 88]
//二分搜索技术,前提要排好序
int i = Arrays.binarySearch(arr, 00);
System.out.println(i); //-1
int i1 = Arrays.binarySearch(arr, 50);
System.out.println(i1); //-5 表示:-(应该插入的位置的索引+1)
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
package com.arrays;
import java.util.Arrays;
import java.util.Comparator;
//comparator比较器对象
public class Test1 {
public static void main(String[] args) {
int [] arr={12,56,32,23,78,19};
//Array的sort方法默认升序排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); //[12, 19, 23, 32, 56, 78]
Integer [] arr1={12,56,32,23,78,19};
Arrays.sort(arr1, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
//指定规则
/*if (o1>o2){
return 1;
}
else if (o1<o2){
return -1;
}
return 0;*/
//return o1-o2; //默认升序 // //[12, 19, 23, 32, 56, 78]
return o2-o1; //降序 //[78, 56, 32, 23, 19, 12]
}
});
System.out.println(Arrays.toString(arr1));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
package com.arrays;
public class Student {
String name;
int age;
double height;
public Student() {
}
public Student(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
package com.arrays;
import java.util.Arrays;
import java.util.Comparator;
public class StudentTest {
public static void main(String[] args) {
Student[] students=new Student[4];
students[0]=new Student("小马哥",23,180.5);
students[1]=new Student("小飞侠",32,183.5);
students[2]=new Student("马奎斯",40,170.5);
students[3]=new Student("罗西",20,190.5);
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
//制定排序规则
//按照年龄升序
//return o1.getAge()-o2.getAge(); //[Student{name='罗西', age=20, height=190.5}, Student{name='小马哥', age=23, height=180.5}, Student{name='小飞侠', age=32, height=183.5}, Student{name='马奎斯', age=40, height=170.5}]
//按照年龄降序
//return o2.getAge()-o1.getAge(); //[Student{name='马奎斯', age=40, height=170.5}, Student{name='小飞侠', age=32, height=183.5}, Student{name='小马哥', age=23, height=180.5}, Student{name='罗西', age=20, height=190.5}]
//按照身高升序排序
//return Double.compare(o1.getHeight(),o2.getHeight()); //[Student{name='马奎斯', age=40, height=170.5}, Student{name='小马哥', age=23, height=180.5}, Student{name='小飞侠', age=32, height=183.5}, Student{name='罗西', age=20, height=190.5}]
//按照身高降序
return Double.compare(o2.getHeight(),o1.getHeight()); //[Student{name='罗西', age=20, height=190.5}, Student{name='小飞侠', age=32, height=183.5}, Student{name='小马哥', age=23, height=180.5}, Student{name='马奎斯', age=40, height=170.5}]
}
});
System.out.print(Arrays.toString(students));
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
文章来源: blog.csdn.net,作者:不会压弯的小飞侠,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_43514330/article/details/125144484
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)