Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java-从链表中查找最大值_Java_Object_Linked List_Find_Max - Fatal编程技术网

java-从链表中查找最大值

java-从链表中查找最大值,java,object,linked-list,find,max,Java,Object,Linked List,Find,Max,免责声明:我是一名很早的学生,正在努力学习java。如果我遗漏了任何重要信息,请告诉我 我正在编写一个程序,提示用户对链表进行各种操作(添加、删除、更改值等),但不是存储字符串或某些基本数据类型,而是存储Student类型的对象(基本上包含一个字符串表示学生的姓名,一个整数表示他们的考试分数)我被困在如何找到最高分数的问题上,因为我无法找到最高的学生 任何帮助都将不胜感激。您可以有两个变量,一个作为currentScore,另一个作为newScore。然后遍历每个学生对象,得到测试值,然后进行比

免责声明:我是一名很早的学生,正在努力学习java。如果我遗漏了任何重要信息,请告诉我

我正在编写一个程序,提示用户对链表进行各种操作(添加、删除、更改值等),但不是存储字符串或某些基本数据类型,而是存储Student类型的对象(基本上包含一个字符串表示学生的姓名,一个整数表示他们的考试分数)我被困在如何找到最高分数的问题上,因为我无法找到最高的学生


任何帮助都将不胜感激。

您可以有两个变量,一个作为currentScore,另一个作为newScore。然后遍历每个学生对象,得到测试值,然后进行比较。如果新分数较低,则保持最新。如果新分数较高,则用新分数替换当前分数,并继续遍历。当您遍历列表时,您的得分最高

您可以按照所述的其他答案在列表中迭代,也可以使用Collections.max方法。要使用此方法,您的学生类应该实现接口

public class Student implements Comparable<Student>
公共班级学生实施可比
您需要将compareTo方法添加到类中:

@Override
public int compareTo(Student student)
{
    if (this.score > student.score)
    {
        return 1;
    }
    if (this.score < student.score)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}
@覆盖
公共国际比较(学生)
{
如果(this.score>student.score)
{
返回1;
}
如果(this.score

现在,当您编写Collections.max(list)
时,您将得到得分最高的学生。

我编写了一个与您的案例相匹配的简单程序

主类:

import java.util.*;
import java.lang.Math; 

public class FindHighestScore
{
  public static void main(String[] args)
  {
    LinkedList<Student> studentLinkedlist = new LinkedList<Student>();

    studentLinkedlist.add(new Student("John",1)); // Adding 5 students for testing
    studentLinkedlist.add(new Student("Jason",5));
    studentLinkedlist.add(new Student("Myles",6));
    studentLinkedlist.add(new Student("Peter",10)); // Peter has the highest score
    studentLinkedlist.add(new Student("Kate",4));

    int temp = 0; // To store the store temporary for compare purpose
    int position = 0; // To store the position of the highest score student

    for(int i = 0; i < studentLinkedlist.size(); i ++){
      if(studentLinkedlist.get(i).getScore() > temp){ 
        temp = studentLinkedlist.get(i).getScore(); 
        position = i; 
      }
    }

  System.out.println("Highest score is: " + studentLinkedlist.get(position).getName()); 
  System.out.println("Score: " + studentLinkedlist.get(position).getScore());


  }
}
public class Student
{
  String name;
  int score;

  Student(){
  }

  Student(String name, int score){
    this.name = name;
    this.score = score;
  }

  String getName(){
    return this.name;
  }

  int getScore(){
    return this.score;
  }
}

上述程序产生如下结果:

Highest score is: Peter
Score: 10