我正在尝试更改列表中某个值的值,但我在使用set方法时遇到了麻烦。
List<?> row = rse.next();
index = row.indexOf("nyc");
row.set(index, "New York");所以我从结果集中抓取一行结果到一个ArrayList中,我被迫这样做,所以我使用了一个ArrayList,或者一个数组。我必须使用List。
我知道这些值都是字符串,有没有办法在列表中找到"nyc“,并将其替换为"New York”。目前,上面的代码给出了这个错误:
The method set(int, capture#4-of ?) in the type List<capture#4-of ?> is not
applicable for the arguments (int, String) 发布于 2013-06-15 00:30:48
你的引用List<?>是一个通配符类型,如果你知道列表是字符串的,那么就把它改为List<String>,并且它应该可以工作。
发布于 2013-06-15 00:42:19
根据Java文档,可以修改使用通配符键入的集合,因为您可以知道该集合可以支持的真实类型。
您可以使用super关键字来检索集合中的边界。
List<? super String> list = new ArrayList<>();
list.add( new String( "one" ) );
list.set( 0,"two" );访问:http://docs.oracle.com/javase/tutorial/java/generics/
https://stackoverflow.com/questions/17113141
复制相似问题