簡體   English   中英

在鏈表中查找方法

[英]Find method in Linked List

我有一個名為ll的對象的鏈表。 我的對象稱為“ 學生” ,它具有名稱,年級和年齡。 我想創建一個基於學生姓名的查找方法。

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

在find方法中,它說找不到符號ll(int)。 我該怎么做才能根據我的對象的參數創建查找方法。

要從列表中獲取第n個元素,請使用get方法: ll.get(e)而不是ll(e)

另一方面,您要遍歷該列表,並且不重復在鏈接列表上調用get更為有效。 相反,您可以這樣做:

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

要使現有方法起作用,您需要更改:

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

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

我們需要使用get()方法從列表中獲取元素,它不同於數組。

另外,如果您想要一種按姓名查找學生的方法,則可以編寫如下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM