相关疑难解决方法(0)

在调用instanceof之前需要进行空检查吗?

null instanceof SomeClass退还false还是扔NullPointerException

java null nullpointerexception

1287
推荐指数
7
解决办法
22万
查看次数

为什么Java Collections不删除泛型方法?

为什么Collection.remove(Object o)不是通用的?

似乎Collection<E>可能有boolean remove(E o);

然后,当您意外地尝试Set<String>从a中删除(例如)而不是每个单独的String时Collection<String>,这将是编译时错误,而不是稍后的调试问题.

java generics api collections

140
推荐指数
5
解决办法
2万
查看次数

Java:instanceof Generic

有没有办法找到泛型的类型?

if (T instanceof String) {
    // do something...
}
Run Code Online (Sandbox Code Playgroud)

以上绝对不会编译.

java generics instanceof

33
推荐指数
2
解决办法
4万
查看次数

为什么instanceof不能使用Generic?

可能重复:
Java:Instanceof和Generics

我正在尝试编写一个将通用List转换为特定类型List的函数.找到下面的代码

public <T>List<T> castCollection(List srcList, Class<T> clas){
    List<T> list =new ArrayList<T>();
    for (Object obj : srcList) {
       if(obj instanceof T){
            ...
       }
    }
    return list;
}
Run Code Online (Sandbox Code Playgroud)

obj instanceof T显示编译错误 -

无法对类型参数T执行instanceof检查.请改为使用其擦除对象>,因为将在运行时擦除其他泛型类型信息.

任何澄清或获得预期结果的方法?

提前致谢.:)

java generics instanceof typeerror

20
推荐指数
2
解决办法
4万
查看次数

使用泛型从三个相似的方法中创建一个简洁的方法

我已经尝试用泛型做一些事情,但似乎我个人找不到任何简单的解决方案。我仍然认为将这 3 种类似的方法保持原样是一种罪过。

    public List<PassengerPlane> getPassengerPlanes() {
        List<PassengerPlane> passengerPlanes = new ArrayList<>();
        for (Plane plane : planes) {
            if (plane instanceof PassengerPlane) {
                passengerPlanes.add((PassengerPlane) plane);
            }
        }
        return passengerPlanes;
    }

    public List<MilitaryPlane> getMilitaryPlanes() {
        List<MilitaryPlane> militaryPlanes = new ArrayList<>();
        for (Plane plane : planes) {
            if (plane instanceof MilitaryPlane) {
                militaryPlanes.add((MilitaryPlane) plane);
            }
        }
        return militaryPlanes;
    }

    public List<ExperimentalPlane> getExperimentalPlanes() {
        List<ExperimentalPlane> experimentalPlanes = new ArrayList<>();
        for (Plane plane : planes) {
            if (plane instanceof ExperimentalPlane) {
                experimentalPlanes.add((ExperimentalPlane) plane);
            } …
Run Code Online (Sandbox Code Playgroud)

java generics

10
推荐指数
1
解决办法
891
查看次数

为什么"t instanceof T"不允许,其中T是类型参数,t是变量?

Eclipse表示,由于泛型橡皮擦,Type Parameter不允许使用instanceof操作.

我同意在运行时,不会保留类型信息.但请考虑以下类的通用声明:

class SomeClass<T>{
    T t;
    SomeClass(Object o){
        System.out.println(o instanceof T);   // Illegal
    }   
}        
Run Code Online (Sandbox Code Playgroud)

在运行时,不会出现T!但是如果我实例化这个类型为Integer的类,那么相应的对象将具有Integer类型的字段t.

那么,为什么我不能用T检查变量的类型,它可以在运行时被Integer替换.而我实际上会做类似"o instanceof Integer"的事情.

在哪些情况下,允许带有类型参数的instanceof会导致故障,从而禁止它?

java generics instanceof type-parameter

6
推荐指数
3
解决办法
5292
查看次数

将instanceof与泛型一起使用

我正在阅读有关泛型的内容,并且了解到它们不允许使用instanceof. 但是,以下代码可以编译:

WeakReference<String> ps = new WeakReference<>(new String());
if (ps instanceof WeakReference<String>) {
    System.out.println("Ok");
}
Run Code Online (Sandbox Code Playgroud)

谁能澄清我不明白的事情吗?

java

5
推荐指数
1
解决办法
244
查看次数