
本文档旨在指导读者如何对 Java 数组中的元素进行排序,并以表格形式输出排序前后的数据。我们将使用选择排序算法对用户输入的测试分数进行排序,并展示如何保留原始索引信息,最终以清晰的表格形式呈现排序结果。本文档将提供详细的代码示例和步骤说明,帮助读者理解并实现数组排序和格式化输出。
理解需求
我们需要对用户输入的测试分数进行排序,并在排序后以表格形式输出。关键在于,需要保留原始输入顺序的索引信息,以便在排序后的表格中正确显示“Grade Number”。这意味着我们不能直接对原始数组进行排序,而需要借助辅助数组来保存索引信息。
解决方案
以下是一种实现该功能的方案:
- 创建索引数组: 创建一个与测试分数数组大小相同的索引数组,初始化为 [0, 1, 2, ..., n-1]。
- 自定义排序方法: 修改原有的selectionSort方法,使其能够根据测试分数数组的值对索引数组进行排序。
- 输出排序后的表格: 遍历排序后的索引数组,并根据索引从测试分数数组中取出对应的值,输出到表格中。
代码实现
import java.util.Scanner;
public class ArrayIntro2 {
public static void main(String[] args) {
// integer array
int[] TestGrades = new int[25];
// creating object of ArrayIntro2T
ArrayIntro2T pass = new ArrayIntro2T(TestGrades, 0, 0, 0);
// getting total and filling array
int scoreCount = ArrayIntro2T.FillArray(TestGrades, 0);
// get average score
double avg = pass.ComputeAverage(TestGrades, scoreCount);
// outputting unsorted table
System.out.println("Original Test Scores:");
ArrayIntro2T.OutputArray(TestGrades, scoreCount, avg);
// Sort and output sorted table
System.out.println("\nTable of sorted test scores");
ArrayIntro2T.OutputSortedArray(TestGrades, scoreCount, avg);
}
}
// new class to store methods
class ArrayIntro2T {
// variable declaration
double CalcAvg = 0;
int ScoreTotal = 0;
int ScoreCount = 0;
int[] TestGrades = new int[25];
// constructor
public ArrayIntro2T(int[] TestGradesT, int ScoreCountT, double CalcAvgT, int ScoreTotalT) {
TestGrades = TestGradesT;
ScoreCount = ScoreCountT;
CalcAvg = CalcAvgT;
ScoreTotal = ScoreTotalT;
}
// method to fill array
public static int FillArray(int[] TestGrades, int ScoreCount) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter test scores one at a time, up to 25 values or enter -1 to quit");
TestGrades[ScoreCount] = scan.nextInt();
if (TestGrades[ScoreCount] == -1) {
System.out.println("You have chosen to quit ");
}
while (TestGrades[ScoreCount] >= 0 && ScoreCount < TestGrades.length -1) {
ScoreCount++;
System.out.println("Enter the next test score or -1 to finish ");
TestGrades[ScoreCount] = scan.nextInt();
if (TestGrades[ScoreCount] == -1) {
break;
}
}
return ScoreCount;
}
// method to compute average
public double ComputeAverage(int[] TestGrades, int ScoreCount) {
for (int i = 0; i < ScoreCount; i++) {
ScoreTotal += TestGrades[i];
CalcAvg = (double) ScoreTotal / (double) ScoreCount;
}
return CalcAvg;
}
public static void selectionSort(int[] arr, int[] indices, int length) {
for (int i = 0; i < length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < length; j++) {
if (arr[indices[j]] < arr[indices[minIndex]]) {
minIndex = j;
}
}
// Swap indices
int temp = indices[i];
indices[i] = indices[minIndex];
indices[minIndex] = temp;
}
}
// method to output scores and average
public static void OutputArray(int[] TestGrades, int ScoreCount, double CalcAvg) {
System.out.println("Grade Number\t\tGrade Value");
for (int i = 0; i < ScoreCount; i++) {
System.out.println((i + 1) + "\t" + "\t" + "\t" + TestGrades[i]);
}
System.out.printf("Calculated Average\t" + "%.2f%%\n", CalcAvg);
}
public static void OutputSortedArray(int[] TestGrades, int ScoreCount, double CalcAvg) {
int[] indices = new int[TestGrades.length];
for (int i = 0; i < TestGrades.length; i++) {
indices[i] = i;
}
selectionSort(TestGrades, indices, ScoreCount);
System.out.println("Grade Number\t\tGrade Value");
for (int i = 0; i < ScoreCount; i++) {
System.out.println((indices[i] + 1) + "\t" + "\t" + "\t" + TestGrades[indices[i]]);
}
System.out.printf("Calculated Average\t" + "%.2f%%\n", CalcAvg);
}
}代码解释
- selectionSort(int[] arr, int[] indices, int length): 这个方法现在接受原始数组 arr,索引数组 indices 和需要排序的数组长度length 作为参数。它根据 arr 中的值对 indices 数组进行排序,使得 indices 数组中的元素按照 arr 中对应位置的值从小到大排列。
- OutputSortedArray(int[] TestGrades, int ScoreCount, double CalcAvg): 这个方法首先创建并初始化索引数组 indices。然后,调用 selectionSort 方法对索引数组进行排序。最后,它遍历排序后的索引数组,并使用索引从 TestGrades 数组中获取相应的值,并以表格形式输出。
- 在 main 方法中,调用 OutputArray 输出原始数组,调用 OutputSortedArray 输出排序后的数组。
运行结果
程序首先会要求用户输入测试分数,然后输出原始的测试分数表格,接着输出排序后的测试分数表格。排序后的表格会显示按照分数从小到大排列的成绩,并保留原始的“Grade Number”。
易通(企业网站管理系统)是一款小巧,高效,人性化的企业建站程序.易通企业网站程序是国内首款免费提供模板的企业网站系统.§ 简约的界面及小巧的体积:后台菜单完全可以修改成自己最需要最高效的形式;大部分操作都集中在下拉列表框中,以节省更多版面来显示更有价值的数据;数据的显示以Javascript数组类型来输出,减少数据的传输量,加快传输速度。 § 灵活的模板标签及模
立即学习“Java免费学习笔记(深入)”;
注意事项
- 代码中假设用户最多输入 25 个测试分数。可以根据实际需求修改数组的大小。
- selectionSort 方法是对选择排序算法的实现。您可以根据需要选择其他排序算法。
- 在实际应用中,可以考虑使用 Java 提供的 Arrays.sort 方法,并结合 Comparator 接口来实现更灵活的排序需求。
总结
通过创建索引数组并对其进行排序,我们可以在对数组元素进行排序的同时,保留原始的索引信息。这种方法在需要维护原始数据顺序的场景下非常有用。本教程提供了一个完整的示例,展示了如何在 Java 中实现数组排序和格式化输出,希望能够帮助读者更好地理解和应用相关技术。









