Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: generic class

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default generic class

    I want to make a generic list class for, let's say, storing a graph. For each node (raging from 1 to n) I keep a linked list for it's neighbors. But I don't want to make an array of lists. I want to use the generic class for making a linked list of linked lists. Roughly speaking, I want to obtain something like this: myList< myList<Integer> > Graph. How can I do it?

    I will post a part of the code, to be more precise:

    class Node<T>{
    	T key;
    	Node<T> prevNode, nextNode;
     
    	Node(T val) {
    		key = val;
    	}
    }
     
    class List<T extends Comparable<T>> {
    	Node<T> FirstNode, LastNode;
     
            void insert(T val) { ... }
            void write() { ... }
    }
     
    class myMain {
    	public static void main(String arg[]) {
     
              List< something > Graph = new List< something >();
              .... 
    } }

    Thank you for your help.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: generic class

        final List<List<Integer>> someList = new ArrayList<List<Integer>>();

    Something like that, you can easily have a list of lists

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    fireatmuself (November 15th, 2009)

  4. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: generic class

    Do you mean
        final ArrayList<List<Integer>> someList = new ArrayList<List<Integer>>();
    ?

    When I write as you said to, I get this error: type parameter List<java.lang.Integer> is not within it's bounds.

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: generic class

    No I mean.

            final List<List<Integer>> someList = new ArrayList<List<Integer>>();
            someList.add(new ArrayList<Integer>());

    That works just fine for me

    // Json

  6. #5
    Junior Member
    Join Date
    Nov 2009
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: generic class

    Now it works. Thank you.

Similar Threads

  1. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 09:18 AM
  2. Generic programming example
    By neo_2010 in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 8th, 2009, 11:38 AM