简体   繁体   English

java-从链接列表中找到最大值

[英]java - find the max value from a linked list

Disclaimer: I am a very early student and am struggling to learn java. 免责声明:我是一个非常早期的学生,正在努力学习Java。 Please tell me if I'm leaving out any important information. 请告诉我是否遗漏任何重要信息。

I am writing a program that prompts the user to do various operations to a linked list (add, remove, change value, etc.) but rather than storing a string or some primitive data type I am storing objects of type Student (which basically contains a string for the name of the student and an integer for their test score) and am stuck on how to find the maximum test score since I can't just find the highest Student. 我正在编写一个程序,提示用户对链接列表进行各种操作(添加,删除,更改值等),但不是存储字符串或某些原始数据类型,而是存储了Student类型的对象(基本上包含一个用于表示学生姓名的字符串和一个用于其测试分数的整数),并且由于我无法找到最高的学生,因此在如何找到最大的测试分数上受了限制。

Any help would be appreciated. 任何帮助,将不胜感激。

Well you can have two variables, one as the currentScore, and another as the newScore. 好了,您可以有两个变量,一个是currentScore,另一个是newScore。 And then traverse through each student object, get the test value, and then compare. 然后遍历每个学生对象,获取测试值,然后进行比较。 If the new score is lower, then keep current. 如果新分数较低,则保持最新。 If new score is higher, replace current score with new score, and keep traversing. 如果新分数更高,请用新分数替换当前分数,并继续遍历。 When you traverse the list, you have the highest score 遍历列表时,您的得分最高

You can iterate over the list as other answers described or you can use Collections.max method. 您可以按照描述的其他答案遍历列表,也可以使用Collections.max方法。 To use this method your Student class should implement comperable interface. 要使用此方法,您的Student类应实现可扩展接口。

public class Student implements Comparable<Student>

and you need to add compareTo method to the class: 并且您需要向类添加compareTo方法:

@Override
public int compareTo(Student student)
{
    if (this.score > student.score)
    {
        return 1;
    }
    if (this.score < student.score)
    {
        return -1;
    }
    else
    {
        return 0;
    }
}

Now when you write Collections.max(list) you will get the Student with the highest score. 现在,当您编写Collections.max(list)您将获得得分最高的学生。

I wrote a simple program that matched your case. 我写了一个简单的程序来匹配您的情况。

Main Class: 主类:

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());


  }
}


The student constructor class: 学生构造函数类:

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;
  }
}


The above program produce result as follows: 以上程序产生的结果如下:

Highest score is: Peter
Score: 10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM