排序数组元素及其索引,并以表格形式输出
技术百科
花韻仙語
发布时间:2025-10-21
浏览: 次 本文旨在提供一个完整的教程,指导读者如何使用Java程序对用户输入的测试分数进行排序,并以表格形式输出。核心内容包括:修改现有的选择排序算法,使其能够正确处理部分填充的数组;以及在不修改`main`方法的前提下,将排序后的测试分数以清晰的表格形式呈现。通过本文,读者将掌握数组排序、索引处理以及格式化输出等关键编程技巧。
问题分析与解决方案
原始代码存在的问题在于:
- 选择排序算法 selectionSort() 默认排序整个数组,而实际有效数据可能只占据数组的一部分。这导致未输入数据的数组位置(默认为0)也被排序,影响了最终的输出结果。
- 输出表格时,需要根据排序后的分数,正确地显示对应的原始“Grade Number”。
解决思路是:
- 修改 selectionSort() 方法,使其只排序有效数据部分。
- 创建一个新的方法,专门用于输出排序后的表格,并保留原始的索引信息。
修改选择排序算法
我们需要修改 selectionSort() 方法,使其只排序 TestGrades 数组中从索引 0 到 ScoreCount - 1 的元素。
public static void selectionSort(int[] TestGrades, int ScoreCount) {
int startScan, index, minIndex, minValue;
for (startScan = 0; startScan < (ScoreCount - 1); startScan++) {
minIndex = startScan;
minValue = TestGrades[startScan];
for (index = startScan + 1; index < ScoreCount; index++) {
if (TestGrades[index] < minValue) {
minValue = TestGrades[index];
minIndex = index;
}
}
TestGrades[minIndex] = TestGrades[startScan];
TestGrades[startScan] = minValue;
}
}关键在于将循环的边界条件从 TestGrades.length 修改为 ScoreCount,确保只对有效数据进行排序。
创建排序后表格输出方法
为了输出排序后的表格,我们需要创建一个新的方法,该方法接受 TestGrades 数组和 ScoreCount 作为输入,并输出排序后的分数及其对应的原始索引。
public static void OutputSortedArray(int[] TestGrades, int ScoreCount) {
// 创建一个数组,保存原始索引
Integer[] indices = new Integer[ScoreCount];
for (int i = 0; i < ScoreCount; i++) {
indices[i] = i;
}
// 使用 Lambda 表达式和 Comparator 对索引数组进行排序
Arrays.sort(indices, (i1, i2) -> TestGrades[i1] - TestGrades[i2]);
System.out.println("\nTable of sorted test scores");
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]]);
}
}这个方法首先创建一个 indices 数组,用于存储原始索引。然后,使用 Arrays.sort 方法,结合 Lambda 表达式,根据 TestGrades 数组中的值对 indices 数组进行排序。排序后,indices 数组中的元素将按照对应的 TestGrades 数组中的值升序排列。最后,遍历排序后的 indices 数组,输出对应的原始索引和分数。
调用新方法
为了在不修改 main 方法的前提下调用新的排序方法,我们可以将调用代码添加到 OutputArray 方法的末尾。
public static void OutputArray(int [] TestGrades,int ScoreCount, double CalcAvg)
{
System.out.println("Grade Number\t\tGrade Value");
for(int i=0; i首先调用selectionSort方法,对数组进行排序。然后调用OutputSortedArray方法输出排序后的数组。
完整代码示例
import java.util.Arrays;
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 table
ArrayIntro2T.OutputArray(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<=25)
{
ScoreCount++;
System.out.println("Enter the next test score or -1 to finish ");
TestGrades[ScoreCount] = scan.nextInt();
}
return ScoreCount;
}
//method to compute average
public double ComputeAverage(int [] TestGrades,int ScoreCount)
{
for(int i=0; i TestGrades[i1] - TestGrades[i2]);
System.out.println("\nTable of sorted test scores");
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]]);
}
}
//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注意事项与总结
-
数组边界检查: 在处理数组时,务必注意数组的边界,避免出现 ArrayIndexOutOfBoundsException 异常。
-
代码可读性: 编写清晰、简洁的代码,添加适当的注释,有助于提高代码的可读性和可维护性。
-
性能优化: 对于大规模数据的排序,可以考虑使用更高效的排序算法,例如归并排序或快速排序。
-
原始索引保存: 在排序过程中,使用索引数组的方式可以有效地保留原始数据的索引信息,这在很多实际应用中非常有用。
通过本教程,你已经学会了如何修改选择排序算法,使其能够正确处理部分填充的数组,并以表格形式输出排序后的分数及其对应的原始索引。这些技巧在处理类似的数据排序和展示问题时非常有用。
# ai
# 排序算法
# java
# 排列
# 格式化输出
# 代码可读性
# 数据排序
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- MAC怎么设置程序窗口永远最前_MAC窗口置顶插件
- Windows11怎样开启游戏模式_Windows
- Windows 11怎么更改锁屏超时时间_Wind
- Win11怎么开启游戏模式_Windows11优化
- php8.4匿名类怎么用_php8.4匿名类创建与
- php485在php5.6下能用吗_php485旧
- C++中的constexpr和const有什么区别
- Win11怎么设置桌面图标间距_Windows11
- php485读数据时阻塞怎么办_php485非阻塞
- php与c语言在嵌入式中有何区别_对比两者在硬件控
- Python随机数生成_random模块说明【指导
- Win11怎么关闭自动维护 Win11禁用系统自动
- Win11怎样安装钉钉客户端_Win11安装钉钉教
- Ajax提交表单PHP怎么接收_处理Ajax发送的
- Win11如何连接Xbox手柄 Win11蓝牙连接
- Win11怎么更改任务栏颜色_Windows11个
- 如何在 Go 中调用动态链接库(.so)中的函数
- 如何使用Golang指针与结构体结合_修改结构体内
- c++中如何求一个数的平方根_c++ sqrt函数
- Win11怎么设置按流量计费_Win11限制后台流
- 静态属性修改会影响所有实例吗_php作用域操作符下
- 如何使用Golang搭建Web开发环境_快速启动H
- Python函数接口稳定性_版本演进解析【指导】
- 如何在Golang中使用time处理时间_Gola
- C++如何解析JSON数据?(nlohmann/j
- Python对象比较与排序_魔术方法解析【教程】
- Drupal 中渲染节点时出现 HTML 标签嵌套
- Win10如何更改电脑休眠时间_Windows10
- Win10系统怎么查看网络连接状态_Windows
- Windows10如何更改桌面图标间距_Win10
- php能控制zigbee模块吗_php通过串口与c
- Windows10怎么卸载预装软件_Windows
- 如何在Golang中编写端到端测试_Golang
- php下载安装后memory_limit怎么设置_
- 如何在Windows上设置闹钟和计时器_系统自带的
- Python代码测试策略_质量保障解析【教程】
- Win11怎么开启智能存储_Windows11存储
- Mac怎么给文件夹加密_Mac创建加密磁盘映像教程
- 如何在Golang中操作嵌套切片指针_Golang
- php下载安装包太大怎么下载_分卷压缩下载方法【教
- php嵌入式日志记录怎么实现_php将硬件数据写入
- c# Task.ConfigureAwait(tr
- php能跑在stm32上吗_php在stm32微控
- c++怎么设置线程优先级与cpu亲和性_c++ 多
- php中$this和::能混用吗_对象与静态作用域
- Windows10如何查看蓝屏日志_Win10使用
- Win11怎么开启移动热点_Windows11共享
- Win11怎么清理C盘系统日志_Win11清理系统
- Python数据抓取合法性_合规说明【指导】
- Python装饰器设计思路_功能增强机制说明【指导

+ 1) + "\t\t\t" + TestGrades[indices[i]]);
}
}
//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
QQ客服