Contents
  1. 1. Java Arrays学习笔记
    1. 1.1. 基本排序.sort()方法
    2. 1.2. 引用类型

Java Arrays学习笔记

八种基本数据类型:

int 、short、float、double、long、byte、boolean、char

除了其中的boolean类型外,对于其它其中定义的Arrays都可以使用自带的.sort()方法进行排序(默认为升序排序).

基本排序.sort()方法

E.g.1

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
package javaClassExercise.arrays;
import org.junit.Test;
import java.util.Arrays;
/**
* Created by lomo.
*/
public class arraySortStudy {
@Test
public void intDataSort() {
int[] data = {1,0,99,10,33,67,88,91,9,7,45,12};
Arrays.sort(data);
for (int i : data) {
System.out.println(i);
}
}
//0 1 7 9 10 12 33 45 67 88 91 99
@Test
public void charDataSort() {
char[] data = {'A', 'g', 'c', 'D', 'C', 'f', 'F', 'Y'};
Arrays.sort(data);
for (char s : data) {
System.out.println(s);
}
}
// A C D F Y c f g
}

引用类型

一、 referenceTypeSort.java

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
package javaClassExercise.referencesSort;
import org.junit.Test;
/**
* Created by lomo.
*/
public class referenceTypeSort {
@Test
public void test() {
User user = new User("lomo", "male", 25);
System.out.println(user.getName() + " Is " +user.getGender()); //lomo Is male
}
}
//User 类
class User {
private String name;
private String gender;
private int age;
//cmd+n 生成带参的构造函数, new时必须给参数, new完后就可以使用获得对应属性
public User(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public void setName() {
this.name = name;
}
public String getName() {
return name;
}
public void setGender() {
this.gender = gender;
}
public String getGender() {
return gender;
}
public void setAge() {
this.age = age;
}
public int getAge() {
return age;
}
}



二、 UserComparator类

实现Comparator接口

UserComparator.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package javaClassExercise.referencesSort;
import java.util.Comparator;
/**
* Created by lomo.
*/
//User类的排序
public class UserComparator implements Comparator<User> {
@Override
public int compare(User o1, User o2) {
return o1.getAge() - o2.getAge();
}
}



三、 referencesTypeSortTest类

测试 + 测试结果

referencesTypeSortTest.java

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
package javaClassExercise.referencesSort;
import org.junit.Test;
import java.util.Arrays;
/**
* Created by lomo on.
*/
public class referencesTypeSortTest {
public static void main(String[] args) {
// User users = new User("lomo2", "female", 33);
// System.out.println(users.getAge()); //33
User[] users = new User[]{ new User("lomo", "male", 22),
new User("lomo2", "female", 20), new User("cd", "gender", 18)};
Arrays.sort(users, new UserComparator());
for (User user : users) {
System.out.println("姓名:" + user.getName() + " 性别:" + user.getGender() + " 年龄:" + user.getAge());
}
/** 输出:
姓名:cd 性别:gender 年龄:18
姓名:lomo2 性别:female 年龄:20
姓名:lomo 性别:male 年龄:22
*/
}
@Test
public void testReferenceTypeSort() {
User[] users = new User[]{new User("name", "male", 22), new User("name2", "female", 18)};
Arrays.sort(users, new UserComparator());
for (User user : users) {
System.out.println(user.getGender());
}
//输出:
/**
* female
male
*/
for (User user : users) {
System.out.println(user.getName());
}
//输出:
/**
* name2
name
*/
}
}

Summary

通过对Test测试,发现Arrays.sort排序好像是以其中的年龄进行升序排序,其实不,是因为:UserComparator类的排序方法声明就是以User类中的Age字段进行排序UserComparator类,其实现的是接口:Comparator,该接口中的方法compare声明如下:int compare(T o1, T o2);

[接口中的方法默认为:public abstract修饰!]


Creative Commons License
本文由Lomo创作和发表,采用Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License进行许可.
转载请注明作者及出处,本文作者为Lomo,本文标题为java references type sort.
Contents
  1. 1. Java Arrays学习笔记
    1. 1.1. 基本排序.sort()方法
    2. 1.2. 引用类型