Results 1 to 3 of 3
Thread: Help w/ generics
- 02-15-2008, 11:11 PM #1
Member
- Join Date
- Jan 2008
- Posts
- 8
- Rep Power
- 0
Help w/ generics
I am not to sure if this works. It compiles just fine but is this the correct way to use generics?
The assignment says "Write a generic method numberOf that takes an element and a list and returns the number of times the element appears in the list. Note the element may be of different type than the type of the entries in the list."
Java Code:package listUtilities; import utilities.containers.*; public class ListUtil<E extends Comparable>{ /** * Returns the number of times a generic element appears in the list. */ public static <E> int numberOf(E element, List<? extends Comparable> list){ int n = 0; for(int i = 0;i<list.size();i++){ if ((list.get(i).getClass()).equals(element.getClass()) //check class equality &&list.get(i).compareTo(element)==0) //check value equality n++;//increment counter } return n; }//end of numberOf }//end of class ListUtil
- 02-16-2008, 03:01 AM #2
I'm not sure about your logic as I haven't examined it thoroughly, so I don't know if it will return the correct values. But your use of Generics looks syntactically correct... someone correct me if I'm wrong....is this the correct way to use generics?Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-16-2008, 03:08 AM #3
Similar Threads
-
Generics
By sireesha in forum New To JavaReplies: 2Last Post: 01-10-2008, 11:08 PM -
Generics
By eva in forum New To JavaReplies: 2Last Post: 01-04-2008, 09:10 PM -
Java confused over Generics?
By Bibendum in forum New To JavaReplies: 3Last Post: 12-26-2007, 06:23 AM -
Java Generics (an introduction)
By Java Tutorial in forum Java TutorialReplies: 0Last Post: 11-27-2007, 06:50 PM -
ArrayList<type> - Generics
By Java Tip in forum Java TipReplies: 0Last Post: 11-14-2007, 03:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks