public class Student { private String name; private int age; public Student(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
import java.util.LinkedList; /* * Storing user-defined class objects. */ public class LinkedListExample { public static void main(String[] args) { LinkedList<Student> linkedList = new LinkedList<Student>(); Student john = new Student("John", 32); Student david = new Student("David", 42); Student peter = new Student("Peter", 52); linkedList.add(john); linkedList.add(david); linkedList.add(peter); /* * Using for each loop getting each student object from the linkedList */ for (Student student : linkedList) { System.out.println(student.toString()); } } }
Student [name=John, age=32] Student [name=David, age=42] Student [name=Peter, age=52]
https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoUserDefined.zip?attredirects=0&d=1
See also:
No comments:
Post a Comment