我试图在没有比较器或数组列表的情况下对此对象数组进行排序。在数组中排序以获得平均分数。这里如何在主方法中使用如果有人能给我一个有用的想法我真的不想使用数组列表或比较器,所以我想我需要算法来帮助我,但我正在努力找出一个副本权利保留给Devan Underwood:
public void calcAvgScore()
{
avgScore = (test1 + test2) / 2;
}
然后在构造函数中是:
public Student(String newName, int newId, int newTest1, int newTest2)
{
super(newName, newId);
test1 = newTest1;
test2 = newTest2;
calcAvgScore();
calcGrade();
}
然后:
public static void main(String[] args)
{
Student[] score = new Student[5];
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your students first name, id number, and two test scores");
String newName = keyboard.next();
int newId = keyboard.nextInt();
int newTest1 = keyboard.nextInt();
int newTest2 = keyboard.nextInt();
Student student1 = new Student(newName, newId, newTest1, newTest2);
System.out.println("Please enter your second students first, id number and two test scores");
String newName2 = keyboard.next();
int newId2 = keyboard.nextInt();
int newTest12 = keyboard.nextInt();
int newTest22= keyboard.nextInt();
Student student2 = new Student(newName2, newId2, newTest12, newTest22);
System.out.println("Please enter your third students first name, id number, and two test scores");
String newName3 = keyboard.next();
int newId3 = keyboard.nextInt();
int newTest13 = keyboard.nextInt();
int newTest23= keyboard.nextInt();
Student student3 = new Student(newName3, newId3, newTest13, newTest23);
System.out.println("Please enter your fourth students first name, id number, and two test scores");
String newName4 = keyboard.next();
int newId4 = keyboard.nextInt();
int newTest14 = keyboard.nextInt();
int newTest24= keyboard.nextInt();
Student student4 = new Student(newName4, newId4, newTest14, newTest24);
System.out.println("Please enter your fifth students first name, id number, and two test scores");
String newName5 = keyboard.next();
int newId5 = keyboard.nextInt();
int newTest15 = keyboard.nextInt();
int newTest25= keyboard.nextInt();
Student student5 = new Student(newName5, newId5, newTest15, newTest25);
score[0] = student1;
score[1] = student2;
score[2] = student3;
score[3] = student4;
score[4] = student5;
System.out.println("____________");
max = score[0];
int i;
for(i =0; i < score.length; i++)
{
System.out.println(score[i]);
System.out.println("____________");
}
}
}
答案 0 :(得分:0)
假设您选择了必要的排序算法分区排序,合并排序等,关键是您有一个数组,并且您希望交换数组中的元素以使排序起作用。给定java 按值传递编写一个传递数组元素的方法,因为得分[i]和得分[j]将不会提供所需的效果。 如下所示对调用者
没有任何影响Student sArr[] = ...
swap(sArr[i], sArr[j])
然而,您可以将引用传递给要交换的数组和索引,如下所示
Student sArr[] = ...
swap(sArr, i, j);
或者,您可以使用实现排序逻辑的相同方法执行所有交换,而不调用另一个交换方法来移动事物。希望这有帮助
为了让您了解排序算法,例如使用整数进行shell排序,您可以执行以下操作(不是最有效的,但可以帮助您入门)
// implement findMin and then do shellsort as below
public static void isort(int arr[]) {
for (int i = 0; i < arr.length; i++) {
int mIndex = findMin(arr, i, arr.length);
if (mIndex != i) {
int temp = arr[mIndex];
arr[mIndex] = arr[i];
arr[i] = temp;
}
}
}
根据您案例中学生的平均分数修改您的逻辑以使用比较。您还应该搜索stackoverflow以了解排序算法。希望这有帮助
答案 1 :(得分:0)
就像这样:您可以在没有Comparator
的情况下排序,而是自己实施。换句话说,实际上你不能。据我所知,算法无法帮助你。如果你没有平均分数,你如何填充阵列进行排序?
Student
课程的平均成绩,就像您一样。 (虽然我认为你可能不得不改变构造函数。最好提供Student
类的详细信息。)student.avgScore
进行排序。像:
max = score[0];
int x = 0;
Student temp = null;
for (int i=0; i < score.length; i++) {
x = score[i].avgScore;
temp = score[i];
if (x < score[i+1].avgScore ) {
score[i] = score[i+1];
score[i+1] = temp;
}
}
只是一个粗略的想法。算法可加快排序速度,但您可以使用实际数据进行排序。
答案 2 :(得分:-1)
这看起来你有想要按avgScore排序的学生数组,但你不想使用ArrayList或Comprator。如果你的排序是基于abgscore那么你可以使用任何短路Ex bobble。破坏,快速或合并排序算法