Hi, I am starting out with linkedlists and would like to know about using another class in a linkedlist.
Here is what I am trying to do:

LinkedList<Student> list = new LinkedList<Student>();
list.add(new Student("Name", 92, 87, 81));

I have no idea what the student class would look like! Would anyone mind writing a simple student class for me? (by the way, the numbers are marks). I tried making a class called Student and I put in a a method called Student, but I am really lost as to how to use a class in a linkedlist. Thanks in advance!

Dani AI

Generated

— you were close; is right that you first define a Student type and then put Student instances into the list. Below is a compact, practical Student implementation (save as Student.java). It shows proper encapsulation, a constructor with parameters (so you can call new Student(...)), a handy getAverage() method, and overridden toString(), equals() and hashCode() so collection operations (like contains/remove) behave predictably.

public class Student {
    private String name;
    private int mark1;
    private int mark2;
    private int mark3;

    public Student(String name, int mark1, int mark2, int mark3) {
        this.name = name;
        this.mark1 = mark1;
        this.mark2 = mark2;
        this.mark3 = mark3;
    }

    public String getName() { return name; }
    public double getAverage() { return (mark1 + mark2 + mark3) / 3.0; }

    @Override
    public String toString() {
        return name + " [" + mark1 + ", " + mark2 + ", " + mark3 + "] avg=" + String.format("%.2f", getAverage());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Student)) return false;
        Student s = (Student) o;
        return name.equals(s.name) && mark1 == s.mark1 && mark2 == s.mark2 && mark3 == s.mark3;
    }

    @Override
    public int hashCode() {
        int r = name.hashCode();
        r = 31 * r + mark1;
        r = 31 * r + mark2;
        r = 31 * r + mark3;
        return r;
    }
}

Example usage with a LinkedList and iteration:

import java.util.List;
import java.util.LinkedList;

List<Student> students = new LinkedList<Student>();
students.add(new Student("Alice", 78, 85, 92));
students.add(new Student("Bob", 90, 72, 88));

for (Student s : students) {
    System.out.println(s); // uses toString()
}

Notes: constructors must not declare a return type (e.g., don’t write public void Student(...)). Keep fields private and provide getters/setters as needed. Overriding equals/hashCode is useful when you want contains or remove(Object) to work by value. Use the List interface (List<Student>) so you can swap implementations (LinkedList vs ArrayList) later without changing call sites.

Recommended Answers

All 2 Replies

The code that you written would work, provided that you have a Student. class. Usually first you write the Student class. So in a separate file on its own, create a Student class with any attributes, methods and constructors you want. Then create student instances and put them in the list:

public class Student {
   // attributes
   
   public Student() {

  }

  //  and more
}

Take a look some tutorials about objects

The code that you written would work, provided that you have a Student. class. Usually first you write the Student class. So in a separate file on its own, create a Student class with any attributes, methods and constructors you want. Then create student instances and put them in the list:

public class Student {
   // attributes
   
   public Student() {

  }

  //  and more
}

great thanks alot, that was much easier than i thought :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.