简体   繁体   中英

Find method in Linked List

I have a linked list with objects called ll . My object is called Student which has a name, a grade and an age. I want to create a find method based on the student's name.

import java.util.LinkedList;
import java.util.Scanner;

public class Course {
    LinkedList<Object> ll = new LinkedList<Object>();
    Scanner s = new Scanner(System.in);

    public void addStudent() {
        p("Enter the name that you want");
        String f = s.nextLine();
        Student a = new Student(f);
        ll.add(a);
    }

    public void changeName() {                           //this method is to change the name of a student
        Student student = findStudent();
        p("Enter the name that you want");
        String newName = s.nextLine();
        Student.setName(newName);
    }

    public void setGrade() {                 //this method is to put the student's grade
        Student student = findStudent();
        p("Enter the grade that you want");
        int grade = s.nextInt();
        Student.setGrade(grade);
    }

    public void setAge() {                 //This method is to put the student's grade
        Student student = findStudent();
        p("Enter the age that you want");
        int age = s.nextInt();
        Student.setAge(age);
    }

    public Student findStudent(){
        p("Which student do you want to change? Please enter their name:");
        String name = s.nextLine();
        boolean a=false;
        for (int e=0; e<ll.size(); e++) {
            if (name.equals(ll(e).name)) {
                p("Found the student"); 
                break;
                return e;
                //Find student in the list - left for the author
            } else {
                a=true;
            }
        }
        if (a) {
            p("Student is not in the list");
            Student student = null;
            return student;}
    }

    public String show() {
        if (ll!=null) {
            for (int i=0; i<ll.size(); i++) {
                return "Student(name:" + name + ", age:" + age + ", grade:" + grade + ")";
            }
        } else {return "There are no students in the list";
        }
    }
    //-------------------------------------------------------------------------
    public void p(String x) {
        System.out.println(x);
    }

    public static void main(String[] args) {
        p("What do you want to do?");
        Course course = new Course();
        p("To add a student press 1");
        p("To change the name of a student press 2");
        p("To set the grade for a student press 3");
        p("To set the age of a student press 4");
        p("To find a student press 5");
        p("To show the students press 6");
        p("To get the age of a student press 7");
        p("To get the grade of a student press 8");
        int a = s.nextInt();
        if (a==1) {
            addStudent();
        } else if (a==2) {
            changeName();
        }else if (a==3) {
            setGrade();
        }else if (a==4) {
            setAge(); 
        } else if (a==5) {
            findStudent();
        }else if (a==6) {
            show();
        } else if (a==7) {
            getAge();
        } else if (a==8) {
            getGrade();
        }
        new Course(); 
    }
}

In the find method, it says that it can not find the symbol ll(int). What can i do to make a find method based on a parameter of my Object.

To get the n'th element from a list, use the get method: ll.get(e) instead of ll(e) .

On the other hand, you are iterating over the list, and it's more efficient to not call get repeatedly on a linked list. Instead you can do this:

    int e = 0;
    for (Student s: ll) {
        if (name.equals(s.name)) ....
        e++;
    }

To make the existing method work, you need to change :

if (name.equals(ll(e).name)) {

to

if (name.equals(ll.get(e).name)) {

We need to use get() method to get an element from a list, it's different from an array.

Also, if you want a method to find student by name, you can write one as follows:

public Student findStudent(String name){
        for (int e=0; e<ll.size(); e++) {
            if (ll.get(e).name.equals(name)) {
                return e;
            } 
        }
        //Not found
        return null;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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