java - 通过instanceof使用数组的元素

标签 java arrays instanceof

是否可以将instanceof与两个不同数组中的两个元素一起使用?

例如:if(arrayA[c] instanceof arrayB[c]){}

我尝试了上面的代码,但出现以下错误:

error: ']' expected: if(arrayA[c] instanceof arrayB[c]){}
                                                    ^
error: ';' expected: if(arrayA[c] instanceof arrayB[c]){}
                                                     ^
error: illegal start of expression: if(arrayA[c] instanceof arrayB[c]){}
                                                                     ^

最佳答案

您可以使用Class.isAssignableFrom() .

来自javadoc:

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter.

关于java - 通过instanceof使用数组的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24490892/

相关文章:

Java 使用instanceof 但基于方法参数

java - 多态性和混淆实例

java - 从文本字段输入中提取数据库条目的最佳方法

java - 如何在 jna 中实现具有联合类型的结构体 sizeof()

java - 如何更新 Cucumber-JVM 的 --format 选项?

java - 带数组的平均值和标准差计算器

java - 避免将 InstanceOf 与访问者模式一起使用 - Java

java - 从 struts2 操作多个 contentType 流视频?

javascript - 为什么输入错误: error: Uncaught TypeError: Cannot read property 'indexOf' of undefined

javascript - 从函数返回的数组 - TypeError : callback is not a function