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: How to use Generics (1 generic anyway)

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile How to use Generics (1 generic anyway)

    Generic classes like

    Vector<> , ArrayLIst<>, Set<> , HashSet<>, Collection<>, LinkedList<>, TreeSet<>, and Tree<>

    all are generic. (Ones like Map<><>, HashMap<><> , and TreeMap<><> I don't know much about other than that they use tables like Hash Tables, which I really don't know much about yet. Sorry.)

    These store Object and its subclasses. They don't store primitives but do store

    Integer, Character, Double, Float, Long, Short, Byte, Boolean, String, and other types like that.

    Unlike arrays, these guys can be expanded or decreased in size.

    Kind of like arrays, these classes can also store other Generic, or even of the same type, inside of them.

    ArrayLists, LinkedLists, and Vectors have methods to get the data at them at a specific index.

    Like arrays, data for all these Generics start with index 0.

    Classes like Set and HashSet and TreeSet don't have a way of directly accessing the data in them, except maybe printing out all the data in a toString(). However, using an Iterator, you may be able to do this somehow...though I'm not fully sure how exactly as I haven't used them much myself.

    TreeSets and Trees like binary search trees require the generic parameter (Usually I've use a T) to implement comparable.

    This is done using

    ClassName<T extends Comparable>

    For some reason you have to use extends for both extending an implementing.

    Also,

    ClassName<T> extends Comparable is the class in general, not the parameter being made to have to implement Comparable..

    You can extend more than one as long as only 1 at most is a class.

    There's also the notoation

    <?T?> but I don't know much about that.

    These generics in general are usually set up in some format like this

    GenericClass<ClassName> gc = new GenericClass<ClassName>(params);


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to use Generics (1 generic anyway)

    Is there a question associated with this?

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to use Generics (1 generic anyway)

    Quote Originally Posted by helloworld922 View Post
    Is there a question associated with this?
    My thoughts exactly. I would hope this isn't supposed to be some sort of how to...because not only is it difficult to follow, but some of this information is incorrect.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to use Generics (1 generic anyway)

    What does the <?T?> do?

    It was originally going to be a how to(What's incorrect? If I was wrong anywhere...I'd like to know.)

    Sets don't have a get() method.

    1.) How would you get the stuff at each index then?

    2.) What are double Generics used for and how do they work?

    3.) What info was incorrect? (Maybe the stuff about Sets...but by directly access...I meant something like a get method, i.e., not having to use an Iterator...whatever that is. Learned a bit about it but can't really recall much.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to use Generics (1 generic anyway)

    It was originally going to be a how to(What's incorrect? If I was wrong anywhere...I'd like to know.)
    Constructive criticism: a good how to will require much more than this. There isn't even any code demonstrations, a reasoning behind what generics actually accomplish, when they should be used, how to write classes which utilize the power of generics. I misread quite a bit of this post when I first read this (sorry, but it wasn't that hard to misread) and jumped to the gun that there were several mistakes (let be frank, you do have a tendency to provide incorrect advice). After going through it a bit more thoroughly I'd say its incorrect in calling it a how to and partially incorrect as the direct access of the contents of a Set - this could be debatable, but shows a slight mis-understanding on your part of what exactly a Set is.
    My suggestion: if you want to write a how to I suggest you follow the outline some of the fantastic ones that can be found in the code snippets and tutorials thread, know your subject, write clear and concise, and if you don't know what something is (case in point Iterator) then look it up. How to's should NOT contain lines like "you may be able to do this somehow...though I'm not fully sure how exactly"
    Last edited by copeg; February 7th, 2011 at 06:21 PM.

Similar Threads

  1. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 07:08 PM
  2. Not Generic; cannot be parameterized
    By javaoo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2010, 09:53 AM
  3. Generic method using an int[] argument
    By dubois.ford in forum Collections and Generics
    Replies: 2
    Last Post: March 18th, 2010, 07:18 AM
  4. generic class
    By fireatmuself in forum Collections and Generics
    Replies: 4
    Last Post: November 17th, 2009, 06:06 AM
  5. Generic programming example
    By neo_2010 in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 8th, 2009, 11:38 AM