添加方法链接列表

时间:2016-04-12 21:38:16

标签: java object linked-list

我创建了一个包含对象的链表。该对象称为学生,有三个参数:姓名,年龄,年级。这是我的课程。

链接

class Student {
    private static String name;
    private static int age;
    private static int grade;

    public Student (String n, int a, int g) {
        setName(n);
        setGrade(g);
        setAge(a);
    }

    public static void setName(String n) {
        name=n;
    }

    public static void setGrade(int g) {
        grade=g;
    }

    public static void setAge(int a) {
        age=a;
    }

    public int getAge() {
        return age;
    }

    public int getGrade() {
        return grade; 
    }

    public static String getName() {
        return name;
    }

    public String toString() {
        return "Name: " +name+ " Age: " +age+ " Grade: " +grade; 
    }
}

学生

import java.util.*;
class List {
    private static LinkedList<Student> ll = new LinkedList<Student>();
    private static Link first;
    public List() {
        first=null;
    }

    public static void addStudent() {
        Scanner s = new Scanner(System.in);
        System.out.println("What is the student's name;");
        String n = s.nextLine();
        System.out.println("What is the student's age;");
        int b = s.nextInt();
        System.out.println("What is the student's grade;");
        int g = s.nextInt();
        Student nStudent = new Student(n, b, g);
        Link newStudent = new Link(nStudent);
        newStudent.next=first;
        first=newStudent;
        ll.add(nStudent);
    }

    public static Link findStudent() {
        Scanner sc = new Scanner(System.in);
        System.out.println("What is the student's name?");
        String n = sc.nextLine();
        Link current = first;
        String name = Student.getName();
        while (name!= n) {
            if (current.next==null)
                return null;
            else
                current=current.next;
        }
        return current;
    }

    public static void change () {
        Scanner sc = new Scanner(System.in);
        Link h = findStudent();
        System.out.println("If you want to change his name press 1;");
        System.out.println("If you want to change his age press 2;");
        System.out.println("If you want to change his grade press 3;");
        int l = sc.nextInt();
        if (l==1) {
            System.out.println("What name do you want the student to have?");
            String newname = sc.nextLine();
            Student.setName(newname);

        } else if (l==2) {
            System.out.println("What age do you want the student to have?");
            int j = sc.nextInt();
            Student.setAge(j);
        }
        else if (l==3) {
            System.out.println("What grade do you want the student to have?");
            int e = sc.nextInt();
            Student.setGrade(e);
        }
    }

    public static void show() {
        System.out.println(ll);
    }
}

列表

import java.util.Scanner;
public class Course {
    public static void main (String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int v = sc.nextInt();
        if (v==1) {
            List.addStudent();
        }else if (v==2) {
            List.change();
        }else if (v==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int a = sc.nextInt();
        if (a==1) {
            List.addStudent();
        }else if (a==2) {
            List.change();
        }else if (a==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int r = sc.nextInt();
        if (r==1) {
            List.addStudent();
        }else if (r==2) {
            List.change();
        }else if (r==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int c = sc.nextInt();
        if (c==1) {
            List.addStudent();
        }else if (c==2) {
            List.change();
        }else if (c==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int d = sc.nextInt();
        if (d==1) {
            List.addStudent();
        }else if (d==2) {
            List.change();
        }else if (d==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int x = sc.nextInt();
        if (x==1) {
            List.addStudent();
        }else if (x==2) {
            List.change();
        }else if (x==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int h = sc.nextInt();
        if (h==1) {
            List.addStudent();
        }else if (h==2) {
            List.change();
        }else if (h==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int p = sc.nextInt();
        if (p==1) {
            List.addStudent();
        }else if (p==2) {
            List.change();
        }else if (p==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int m = sc.nextInt();
        if (m==1) {
            List.addStudent();
        }else if (m==2) {
            List.change();
        }else if (m==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int y = sc.nextInt();
        if (y==1) {
            List.addStudent();
        }else if (y==2) {
            List.change();
        }else if (y==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int q = sc.nextInt();
        if (q==1) {
            List.addStudent();
        }else if (q==2) {
            List.change();
        }else if (q==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int w = sc.nextInt();
        if (w==1) {
            List.addStudent();
        }else if (w==2) {
            List.change();
        }else if (w==3) {
            List.show();
        }
        System.out.println("What do you want to do?");
        System.out.println("To add a student press 1");
        System.out.println("To change something press 2");
        System.out.println("To see your students press 3");
        int f = sc.nextInt();
        if (f==1) {
            List.addStudent();
        }else if (f==2) {
            List.change();
        }else if (f==3) {
            List.show();
        }
    }
}

课程

What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
1
What is the student's name;
Tom
What is the student's age;
16
What is the student's grade;
90
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
1
What is the student's name;
Eric
What is the student's age;
16
What is the student's grade;
91
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3
3
[Name: Eric Age: 16 Grade: 91, Name: Eric Age: 16 Grade: 91]
What do you want to do?
To add a student press 1
To change something press 2
To see your students press 3

您会注意到在Course类中,我已多次编写相同的代码,但这是因为我无法找到执行命令后的方法,我会回到主菜单和内容列表不会丢失。 显然,我的add方法是错误的,当我执行程序时输出是这样的:

输出

int x = *776

为什么当我在课堂上添加学生时,它会重复?

3 个答案:

答案 0 :(得分:0)

我认为你的问题是你的Student.class是静态的。

private static String name;
private static int age;
private static int grade;

这部分告诉java,你只需要一个变量/ Class。因此,您的链接列表只存储2个相同静态字段的引用。

您需要删除静态关键字。

答案 1 :(得分:0)

因为您在Student课程中使用静态字段。

private static String name;
private static int age;
private static int grade;

静态字段不属于某个实例,但属于该类本身。 您创建的学生实例数量无关紧要(使用new关键字),static字段的值将在它们之间共享。

您必须删除Student类中的static关键字。 请参阅官方文档:Understanding Class Members

答案 2 :(得分:0)

所有问题都源于您在static课程中使用Student变量的决定。这意味着,无论您创建了多少Student个对象,整个类中每个变量只有一个副本。

将它们设为非static。这意味着您必须在static课程中删除Student的getter和setter。

List课程中,您可以从静态环境访问getter,例如Student.getName()。您需要将对象存储在Link中并将其转换为Student。然后,您就可以在其上拨打getName()

另外,Link类应该是泛型类,例如class Link<T>,因此当您在List课程中使用它时,可以将其用作Link<Student>,因此您在引用{{1}时不必投出它它保持(它将保持Object)。

T中,您需要执行类似的操作 - 关于change找到的Student,请在该学生对象上调用您的setter,而不是在静态调用它上下文。

此外,在findStudentuse equals to compare String values,而不是findStudent!=