簡體   English   中英

是否可以對2D數組的第一列進行排序,但在Java中保持對應的值相同

[英]Is it possible to sort the first column of a 2D array but keep the corresponding values the same in java

我正在用Java創建GradeBook程序,示例輸出如下所示:

在此處輸入圖片說明

我想知道是否可以在保持學生成績的同時對學生姓名進行排序:

我想要它看起來像什么

在此處輸入圖片說明

如果可能的話,請指導我如何做

使用數據結構

此結構將從第二張圖像中打印出您想要的內容。

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Student implements Comparable<Student>{
    private String name;
    private int test;
    private int quiz;

    public Student(String name, int test, int quiz) {
        this.name = name;
        this.test = test;
        this.quiz = quiz;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTest() {
        return test;
    }

    public void setTest(int test) {
        this.test = test;
    }

    public int getQuiz() {
        return quiz;
    }

    public void setQuiz(int quiz) {
        this.quiz = quiz;
    }

    @Override
    public int compareTo(Student o) {
        return this.name.compareTo(o.name);
    }
}

public class Stack {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        students.add(new Student("Willian", 90, 80));
        students.add(new Student("Charles", 70, 95));

        Collections.sort(students);

        System.out.println("\t\t   Test \tQuiz");
        for (Student s : students) {
            System.out.println(String.format("%s\t\t%d\t\t%d", s.getName(), s.getTest(), s.getQuiz()));
        }
    }
}

資源資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM