Results 1 to 3 of 3
  1. #1
    the_rider9 is offline Member
    Join Date
    May 2011
    Posts
    7
    Rep Power
    0

    Default Linked List implementation of various classes

    I am relatively new to Java, and I am currently in a dead end on a program with no idea how to continue. Here is where I am at.

    I implemented a typical singly linked list which contains multiple nodes of class "Student" (1 node per student). However, I need every student to be able to implement a class called "Courses" any amount of time. For example, 1 student in the linked list might have 4 courses (math, bio, phys,english) whereas another student in the linked list might have 23.

    What data structure would be optimal for accessing different courses from different students? How would I tie both classes together in such a fashion?

    Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    What kind of "container" you put the items in would depend on how you want to access them.
    Serially from the beginning: ArrayList
    Randomly by name: Map

  3. #3
    giggs is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    I agree with Norm, data structure will depend on how you will access "Courses". I think ArrayList is a better choice, but this is my personal opinion. Regarding your second question i think you can use something similar to that:
    Java Code:
    import java.util.*;
    public class Student {
    	Student(String aName,String... args)
    	{
    		name=aName;
    		cname=args;
    		for(int i=0;i<cname.length;i++)
    		{
    			cour.add(new Courses(cname[i]));
    		}
    	}
    	public void showAll()
    	{
    		for(int i=0;i<cname.length;i++)
    		{
    			System.out.println(cour.get(i).showCourses());
    		}
    	} 
    	 private String name;
    	 private String[] cname;
    	 private ArrayList<Courses> cour = new ArrayList<Courses>();
    }
    Class "Courses" here has only one method "showCourse()" that returns name of the course.
    Last edited by giggs; 05-21-2011 at 03:53 AM.

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  2. Linked list implementation using core java
    By jhapranay in forum Advanced Java
    Replies: 0
    Last Post: 02-03-2011, 03:15 PM
  3. Storing Mutiple Classes/Data Types in a Linked List
    By Chronoattica in forum New To Java
    Replies: 7
    Last Post: 12-28-2010, 10:03 PM
  4. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  5. Question about linked implementation
    By bdario1 in forum New To Java
    Replies: 5
    Last Post: 04-23-2010, 11:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •