通过显示按数字排序对包含数字和名称数据的对象数组进行排序



包含数字和名称数据的数组如下:

Number          Name
10              Brycen
6               Devan
3               Rylan
1               Gordy
2               Tim
4               Curtis
5               Abe
9               Melvin
8               Ansel
7               Dalton

输出应该是:

Number          Name
1               Gordy
2               Tim
3               Rylan
4               Curtis
5               Abe
6               Devan
7               Dalton
8               Ansel
9               Melvin
10              Brycen

我已经尝试过使用对象数组和FOR..停息——循环。[我].number System.out.println(数据);按顺序显示输出,但对于System.out.println(data[i].name);显示初始状态。

class StudentList { //DaftarSiswa
public int number;
public String name;

public StudentList(int number, String name) {
this.number = number;
this.name   = name;
}
}
class OrderNumber { //NomorUrut
public static void main(String[] args) {
StudentList[] data = new StudentList[10];
data[0] = new StudentList(10, "Brycen");
data[1] = new StudentList( 6, "Devan");
data[2] = new StudentList( 3, "Rylan");
data[3] = new StudentList( 1, "Gordy");
data[4] = new StudentList( 2, "Tim");
data[5] = new StudentList( 4, "Curtis");
data[6] = new StudentList( 5, "Abe");
data[7] = new StudentList( 9, "Melvin");
data[8] = new StudentList( 8, "Ansel");
data[9] = new StudentList( 7, "Dalton");

for (int i = 0; i < data.length; i++) {
for (int j = i + 1; j < data.length; j++) {
int tmp = 0;
if (data[i].number > data[j].number) {
tmp            = data[i].number;
data[i].number = data[j].number;
data[j].number = tmp;
}
}

System.out.println(data[i].name);
}
}
}

你只是交换了数字。您需要交换整个对象:

if (data[i].number > data[j].number) {
Student tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}

您可以使您的Student(从StudentList重命名)类实现Comparable接口,以便使用Arrays对数组进行排序。排序方法。在这种情况下,你必须添加到学生类实现的比较方法。

class Student implements Comparable<Student> { //DaftarSiswa
public int number;
public String name;
public Student(int number, String name) {
this.number = number;
this.name = name;
}
@Override
public int compareTo(Student o) {
return this.number - o.number;
}
@Override
public String toString() {
return "Student {number=" + number + ", name='" + name +"'}n";
}
}
public class Test { //NomorUrut
public static void main(String[] args) {
Student[] data = new Student[10];
data[0] = new Student(10, "Brycen");
data[1] = new Student(6, "Devan");
data[2] = new Student(3, "Rylan");
data[3] = new Student(1, "Gordy");
data[4] = new Student(2, "Tim");
data[5] = new Student(4, "Curtis");
data[6] = new Student(5, "Abe");
data[7] = new Student(9, "Melvin");
data[8] = new Student(8, "Ansel");
data[9] = new Student(7, "Dalton");
Arrays.sort(data);
System.out.println(Arrays.toString(data));
}
}

你可以使用Java Streams,在我看来,这比循环更优雅。

假设StudentList类通常有getter(和setter),你可以这样做:

public class OrderNumber { //NomorUrut
public static void main(String[] args) {
StudentList[] data = new StudentList[10];
data[0] = new StudentList(10, "Brycen");
data[1] = new StudentList( 6, "Devan");
data[2] = new StudentList( 3, "Rylan");
data[3] = new StudentList( 1, "Gordy");
data[4] = new StudentList( 2, "Tim");
data[5] = new StudentList( 4, "Curtis");
data[6] = new StudentList( 5, "Abe");
data[7] = new StudentList( 9, "Melvin");
data[8] = new StudentList( 8, "Ansel");
data[9] = new StudentList( 7, "Dalton");
Arrays.stream(data) // Start stream
.sorted(Comparator.comparingInt(StudentList::getNumber)) // Sort by Number
.forEach(student -> System.out.println(student.getName())); // Output (like you shown in code) 
}
}

如果你也想有一个列表或数组排序的顺序,你可以collect它。

// get sorted list of students
var students = Arrays.stream(data) // Start stream
.sorted(Comparator.comparingInt(StudentList::getNumber)) // Sort by Number
.collect(Collectors.toList()); // collect to list
// if you really want an array
var studentsArray = students.toArray(new StudentList[students.size()]);

相关内容

  • 没有找到相关文章

最新更新