Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通配符java扩展_Java_Wildcard_Extends - Fatal编程技术网

通配符java扩展

通配符java扩展,java,wildcard,extends,Java,Wildcard,Extends,考虑以下代码: List<? extends Integer> lst= new ArraList<Integer>(); lst.add(5);//Compile error lst.get(5);//OK 列表 是一个不同的蛋糕。。。您提供要获取的整数索引。。。(更不用说Sotirios提到的内容:在这种情况下,返回类型将是对象。此外,在这个角色中,列表是提供程序…)除了null之外,不能向由通配符限定的列表添加任何内容,因为您永远不知道基础类型 List<?

考虑以下代码:

List<? extends Integer> lst= new ArraList<Integer>();
lst.add(5);//Compile error
lst.get(5);//OK
列表


是一个不同的蛋糕。。。您提供要获取的整数索引。。。(更不用说Sotirios提到的内容:在这种情况下,返回类型将是
对象
。此外,在这个角色中,列表是提供程序…)

除了
null
之外,不能向由通配符限定的
列表
添加任何内容,因为您永远不知道基础类型

List<? extends Integer> lst= new ArrayList<Integer>();
lst.add(5); // Compile error
lst.get(5); // 5 is just the index
你能补充什么

List<? extends Foo> fooList = new ArrayList<Zoop>(); // could come from a method

列表一旦你有了一个
列表,可能的FYI副本,就需要一个索引。考虑到
整数
最终的
?extends Integer
没有用。@RohitJain:)嘿,你一直在破坏我的游戏:)而你在我打字之前就破坏了我的游戏,所以现在我好像是个剽窃者…@ppeterka66。啊!!我不会再这样做了挑剔:您可以添加
null
。请注意,在上述情况下,
lst.get(x)
返回类型为
object
的对象,因此您可能需要强制转换它。@SotiriosDelimanolis这是真的,而且,在这种情况下,问题是列表是一个提供程序。。。因此,应用这两个集的交集(扩展整数集和超整数集)将留下在本例中使用的确切的整数类型。。。
List<? extends Integer> lst= new ArrayList<Integer>();
lst.add(5); // Compile error
lst.get(5); // 5 is just the index
public class Foo {}

public class Bar extends Foo {}

public class Zoop extends Foo {}
List<? extends Foo> fooList = new ArrayList<Zoop>(); // could come from a method