你如何从数组中取出一个对象并在它上面使用 instanceof ?

Uio443

我正在尝试使以下代码或类似的东西起作用:

static String[][] arr = new String[4][4];
static int[][] arr2 = new int[4][4];



for(int i = 0; i < arr.length; i++){
        for (int j = 0; j < arr[0].length; j++) {
            arr[i][j] = "1";
            arr2[i][j] = Integer.parseInt(arr[i][j]);
            if(arr2[i][j] instanceof int){
                System.out.println("Works");
            }
        }
    }

相反,IDE 将其标记为红色并给出错误“不可转换类型;无法将 int 转换为 int”。

有人可以帮忙吗?

奥哈斯兰奇

您提供的信息有限。你将不得不改变每一个地方,在那里说intInteger,因为instanceof无法检查基本类型。您的代码如下所示:

static String[][] arr = new String[4][4];
static Integer[][] arr2 = new Integer[4][4];


for(int i = 0; i < arr.length; i++){
        for (int j = 0; j < arr[0].length; j++) {
            arr[i][j] = "1";
            arr2[i][j] = Integer.parseInt(arr[i][j]);
            if(arr2[i][j] instanceof Integer){
                System.out.println("Works");
            }
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

什么是克隆对象的instanceof?

来自分类Dev

iframe + instanceof对象失败

来自分类常见问题

检查流中的instanceof

来自分类Dev

在Thymeleaf中使用instanceof

来自分类Dev

使用Jasmine测试instanceof

来自分类Dev

使用instanceof测试接口

来自分类Dev

避免对实体使用instanceof

来自分类Dev

使用for ... in和instanceof解析对象

来自分类Dev

使用for ... in和instanceof解析对象

来自分类Dev

instanceof在JavaScript中如何工作?

来自分类Dev

instanceof在JavaScript中如何工作?

来自分类Dev

为什么Array instanceof对象?

来自分类Dev

如何使InstanceOf与本地存储中的对象一起使用

来自分类Dev

Java中模板类型的instanceof

来自分类Dev

C ++中的instanceof等效项

来自分类Dev

Freemarker中的继承/ instanceof检查

来自分类Dev

通用集合中的ClassCastException与instanceOf

来自分类Dev

C ++中的instanceof等效项

来自分类Dev

通用集合中的ClassCastException与instanceOf

来自分类Dev

引发并捕获异常,或使用instanceof?

来自分类Dev

我在使用instanceof“ wrong”吗?

来自分类Dev

使用“ instanceof”和assertEquals的assertTrue

来自分类Dev

什么是使用“ instanceof”的正确方法?

来自分类Dev

如何检查“?” Class<?> 对象的 instanceof SomeInterface

来自分类Dev

如何在Mockito中模拟instanceof

来自分类Dev

如何在JavaScript中实现instanceof

来自分类Dev

如何在 Java 中检查以下 instanceof?

来自分类Dev

构造函数返回对象的instanceof false

来自分类Dev

对象不是它自己的类的“ instanceof”