Results 1 to 7 of 7
- 06-28-2009, 05:19 PM #1
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Generic Collections---Dysfuntional Java
Hi
Can anyone please tell me why this code does not compile
on the 3 lines where i am adding Strings to the collection?
I think Generics and Collections in Java although good in their goal of achieving type safety, confuse the heck out of the newbie!!!
Thanks in advance:
package com.psg.cert.ch3.wrapper;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.util.Vector;
public class BoolTest {
static List<?> aList=new Vector<String>();
/**
* @param args
*/
public static void main(String[] args) {
aList.add("USA");
aList.add("Russia");
aList.add("UK");
}
}
-
I'm curious, why List<?> and not List<String> here?
- 06-28-2009, 06:18 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
It doesn't compile because the compiler does not know what is in the list, because it is declared as List<?>, and because of that, you cannot add elements to it. You have to change it to List<String> for it to work.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-28-2009, 06:42 PM #4
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Hi Guys
Thanks for the replies.
I just want to understand the usage of "?".
i.e. what context i would use it in...for method parameters only?
Also, let's say that i want a Collection that is heterogeneous...
How would i achieve that with the new Generic Collections.
I know the point is to have type safety, but for argument sakes, how would i get it done?
This should answer the question: Why "?" and not "String"....
regards
beezLast edited by beezerbutt; 06-28-2009 at 06:44 PM.
- 06-28-2009, 06:43 PM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
You rarely really want to use <?>, except occasionally in method parameters. <?> is roughly equivalent to <? extends Object>.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
-
You really don't want this. First of all, it's a bad idea, one that will often cause bad bugs later; second it goes against OOP principles; lastly, why use generics here if you are trying to do this? Why even worry about "type safety" when the collection you are trying to create has no type?
By declaring your List as a List<String>How would i achieve that with the new Generic Collections.
I know the point is to have type safety, but for argument sakes, how would i get it done?
- 06-28-2009, 07:37 PM #7
Member
- Join Date
- Feb 2009
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Collections Help
By Dr Gonzo in forum New To JavaReplies: 0Last Post: 12-07-2008, 09:15 PM -
Collections Sort
By senthil_jr in forum Advanced JavaReplies: 2Last Post: 06-04-2008, 08:11 AM -
Collections framework
By yuvi461 in forum New To JavaReplies: 2Last Post: 01-08-2008, 10:46 AM -
Performance Of Collections
By thomasprabu in forum Advanced JavaReplies: 0Last Post: 01-05-2008, 11:17 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks