mezzodrinker
mezzodrinker

Reputation: 988

List<?> adding and removing elements

I'm currently programming a bukkit plugin (bukkit is a minecraft server type ;) ) and there, you've got a method in a class named YamlConfiguration that has following method:

public List<?> getList(String path)

My problem is: I'd like to add and/or remove elements from this list returned. But when I try to do this via

YamlConfiguration config = YamlConfiguration.load("path/to/config.yml");
config.getList("a.path").add(new String("foo"));

eclipse is throwing an error because

The method add(capture#2-of ?) in the type List<capture#2-of ?> is not applicable for the arguments (String)

I really don't know what to do. ^^'

Regards.

Upvotes: 3

Views: 2480

Answers (5)

Briareos386
Briareos386

Reputation: 1957

Do you really want to load your item with getList? There's also getStringList(...) available.

So maybe you could try:

config.getStringList("a.path").add("foo");

Upvotes: 3

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

List<?> list can point to a List of any type, eg List<String> or List<Integer> or any other. That means you dont know what the actual list is, so you are allowed to add only null to this list. Read more in this tutorial http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html

Upvotes: 2

Erfan Bagheri
Erfan Bagheri

Reputation: 638

I think you have to replace <?> with <Object> then you can add any think like Integer,String,etc to your list ...

good luck :)

Upvotes: 0

user2147970
user2147970

Reputation: 412

If you are sure that it returns a list of String, you can cast it:

((List<String>)config.getStringList("a.path")).add("foo");

Upvotes: 0

Foredoomed
Foredoomed

Reputation: 2239

? is wildcard which represents an unknown type, because you don't know the type of list<?> so you can't add any element into it.

Upvotes: 0

Related Questions