gpt4 book ai didi

java - 检查链表中每个节点的值是否等于 String

转载 作者:行者123 更新时间:2023-11-30 06:12:42 26 4
gpt4 key购买 nike

我正在为家庭作业编写一个程序,该程序具有一个二叉搜索树 (Roster),Student 对象通过其字符串 ID 插入其中。每个学生都有一个链接列表,他们的类(class)被添加到其中,其中包含类(class)的字符串和他们的成绩。二叉搜索树是我自己的实现版本。

我在实现打印包含特定类(class)的学生的方法时遇到问题。我认为我的 printCourseHelper() 方法中的实现已关闭,因为 if 条件无法正常工作来检查每个节点的列表中的值是否等于给定值。

我正在查找所有注册“Math161”类(class)的学生,该学生应该为 3,并且将打印该类(class)学生的字符串 ID。当我运行我的程序时,我没有收到任何错误,但是只有在我的 displayStudents() 之上调用的函数正在打印。

我认为问题出在我的 BST.java、printCourse 和 printCourseHelper 方法中:

public void printCourse(String course) {
printCourseHelper(root, course);
}

public void printCourseHelper(Node n, String course) {
if(n.element.getCourseList().contains(course)) {
System.out.print(n.element.getId() + " ");
}

if (n.left != null) {
printCourseHelper(n.left, course);
}
if (n.right != null) {
printCourseHelper(n.right, course);
}
}

Homework5.class/主要:

import java.util.LinkedList;

public class Homework5 {

static Roster rost = new Roster();

public static void main(String[] args) {
addStudent();
displayAllStudents();
lookupStudent("11114");
addCourse();
displayStudents("Math161");
}

// add students to the roster
static void addStudent() {
rost.addStudent(new Student("11111", "Jon", "Benson"));
rost.addStudent(new Student("11112", "Erick", "Hooper"));
rost.addStudent(new Student("11113", "Sam", "Shultz"));
rost.addStudent(new Student("11114", "Trent", "Black"));
rost.addStudent(new Student("11115", "Michell", "Waters"));
rost.addStudent(new Student("11116", "Kevin", "Johnson"));
}

// display all students in the roster
static void displayAllStudents() {
rost.displayAllStudents();
}

// lookup a student in the roster
static void lookupStudent(String id) {
if (rost.find(id) != null) {
System.out.println(id + " found");
} else {
System.out.println(id + " not found");
}
}

// add courses to the roster
static void addCourse() {
rost.addCourse("11111", new Course("CS116", 80));
rost.addCourse("11111", new Course("Math161", 90));
rost.addCourse("11112", new Course("Math161", 70));
rost.addCourse("11112", new Course("CS146", 90));
rost.addCourse("11112", new Course("CS105", 85));
rost.addCourse("11113", new Course("CS216", 90));
rost.addCourse("11114", new Course("CIS255", 75));
rost.addCourse("11114", new Course("CS216", 80));
rost.addCourse("11114", new Course("Math161", 60));
rost.addCourse("11114", new Course("COMM105", 90));
}

// display students enrolled in a given course id
static void displayStudents(String courseId) {
rost.displayStudents(courseId);
}

// display courses taken by a student
static void displayCourses(String id) {
rost.displayCourses("id");
}

// display the average grade for a student
static void getCourseAverage(String courseId) {
rost.getCourseAverage(courseId);
}

// display the average grade for a student
static void dropCoursesBelow(String id, int grade) {
rost.dropCoursesBelow(id, grade);
}

// drop a course from a student
static void dropCourse(String id, String courseId) {
rost.dropCourse(id, courseId);
}

// change the grade for a student
static void changeGrade(String id, String courseId, int grade) {
rost.changeGrade(id, courseId, grade);
}
}

学生.类(class)

class Student implements Comparable<Student> {

String id;
String firstName;
String lastName;
LinkedList<Course> courses = new LinkedList<>();

Student(String id, String fName, String lName) {
this.id = id;
this.firstName = fName;
this.lastName = lName;
}

public String getName() {
return lastName;
}

public void setName(String lName) {
this.lastName = lName;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
public int compareTo(Student other) {
return this.getId().compareTo(other.getId());
}

public void addCourse(Course course) {
courses.add(course);
}

public LinkedList<Course> getCourseList() {
return courses;
}
}

类(class).class:

class Course {

LinkedList<Course> course = new LinkedList<>();
String id; // course id
int grade;

Course(String id, int grade) {
this.id = id;
this.grade = grade;
}

public String getCourseId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public int getGrade() {
return grade;
}

public void setId(int grade) {
this.grade = grade;
}

}

名册.类(class):

class Roster {

Student root;
int numStudents;
BST<Student> roster = new BST<>();
LinkedList<Course> courseList = new LinkedList<>();

public Roster() {
root = null;
numStudents = 0;
}

public void addStudent(Student st) {
roster.insert(st);
numStudents++;
}

public void displayAllStudents() {
roster.traverse(2);
}

public Student find(String id) {
return roster.find(id);
}

public void addCourse(String id, Course course) {
Student student = roster.find(id);
student.addCourse(course);
}

public void displayStudents(String courseId) {
roster.printCourse(courseId);
}
}

BST.java

class BST<Roster extends Comparable> {

private Node root;

public BST() {
root = null;
}

// Generic find method
public Student find(String id) {
Node current = root;
// Loop until e.compare to current element is not equal to 0
while (id.compareTo(current.element.getId()) != 0) {
//!!! implement
// if e.compare is less than 0 set current to current.left
if (id.compareTo(current.element.getId()) < 0) {
current = current.left;
} // else if current is 0 or greater than 0 set current
// to current.right
else {
current = current.right;
}
// if current is null, return null
if (current == null) {
return null;
}
}
// return current value when loop ends
return current.element;
}

public void insert(Student st) {
Node newNode = new Node(st);
if (root == null) {
root = newNode;
} else {
Node current = root;
Node parent = null;
while (true) {
parent = current;
if (st.getId().compareTo(current.element.getId()) < 0) {
current = current.left;
if (current == null) {
parent.left = newNode;
return;
}
} else {
current = current.right;
if (current == null) {
parent.right = newNode;
return;
}
}
}
}
}

public void printCourse(String course) {
printCourseHelper(root, course);
}

public void printCourseHelper(Node n, String course) {
if(n.element.getCourseList().contains(course)) {
System.out.print(n.element.getId() + " ");
}
if (n.left != null) {
printCourseHelper(n.left, course);
}
if (n.right != null) {
printCourseHelper(n.right, course);
}
}

public void traverse(int traverseType) {
switch (traverseType) {
case 1:
System.out.print("\nPreorder traversal: ");
// call preOrder(root) and implement preOrder()
preOrder(root);
break;
case 2:
System.out.print("\nList of all students: ");
inOrder(root);
break;
case 3:
System.out.print("\nPostorder traversal: ");
// call postOrder(root) and implement postOrder()
postOrder(root);
break;
}
System.out.println();
}

private void inOrder(Node localRoot) {
if (localRoot != null) {
inOrder(localRoot.left);
System.out.print(localRoot.element.getId() + " ");
inOrder(localRoot.right);
}
}

private void preOrder(Node localRoot) {
if (localRoot != null) {
System.out.print(localRoot.element + " ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}

private void postOrder(Node localRoot) {
if (localRoot != null) {
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.element + " ");
}
}
}

class Node {

protected Student element;
protected Node left;
protected Node right;

public Node(Student st) {
element = st;
}
}

最佳答案

问题是您正在尝试查看 Course 类型的 LinkedList 是否包含字符串。 LinkedList 的 contains 方法采用 Object 作为其参数类型,这就是您没有遇到编译问题的原因。

下面的代码片段永远不会成立,因为类(class)永远不会等于字符串。我在这里提到 equal 是因为 LinkedList contains 方法在内部检查您传入的对象与其包含的对象的相等性。

if(n.element.returnList().contains(course)) {
System.out.print(n.element.getId() + " ");
}

使用 map 的可能解决方案

将Student类中的类(class)更改为Map,然后更改IF语句以检查Map是否包含基于类(class)名称的元素。如果 map 包含一个对象,那么学生确实选修了这门类(class)。

class Student implements Comparable<Student> {

String id;
String firstName;
String lastName;
Map<String, Course> courses = new HashMap<>();

Student(String id, String fName, String lName) {
this.id = id;
this.firstName = fName;
this.lastName = lName;
}

public String getName() {
return lastName;
}

public void setName(String lName) {
this.lastName = lName;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

@Override
public int compareTo(Student other) {
return this.getId().compareTo(other.getId());
}

public void addCourse(Course course) {
courses.put(course.getId(), course);
}

public Map<String, Course> getCourses() {
return courses;
}
}

IF语句

if(n.element.getCourses().get(course) != null) {
System.out.print(n.element.getId() + " ");
}

使用列表的可能解决方案

向 Student 类添加新方法。

 public boolean takesCourse(String courseName){
for(Course course : courses){
if(courseName.equals(course.getId)) {
return true;
}
}

return false;
}

IF语句

if(n.element.takesCourse(course)) {
System.out.print(n.element.getId() + " ");
}

关于java - 检查链表中每个节点的值是否等于 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49885590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com