2017-11-23 2 views
-3

내가 해결하려면 다음 코드 또는 유사한 무언가를 만들려고 노력하고 있어요 :배열에서 객체를 어떻게 가져 와서 instanceof를 사용합니까?

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 캐스팅 할 수 없습니다."

누군가 도울 수 있습니까?

+0

INT는 [] – Artemis

+0

을 int로하지 않는 배열은 2 차원의 int 배열로 선언, 그래서 당신은 확인할 수 있습니다 어떤 인덱스가 int 타입입니다, 더'instanceof'이 작동하지 않습니다 원시 타입들. 다음을 참조하십시오 : https://stackoverflow.com/questions/12361492/how-to-determine-the-primitive-type-of-a-primitive-variable * 정말로 * 확인하고 싶습니까? – alfasin

+0

'instanceof'는 char, int, boolean과 같은 일반 오래된 값 유형에 대해서도 사용할 수 있습니까? – selbie

답변

-1

제공하신 정보가 제한되어 있습니다. instanceof은 기본 유형을 확인할 수 없으므로 intInteger으로 바꿔야합니다. 귀하의 코드는 다음과 같습니다

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"); 
      } 
     } 
    } 
+0

Integer.parseInt가 작동하지 않았 으면 예외가 발생하기 때문에 Integer.parseInt가 작동하는지 항상 알 수 있습니다. –

+0

이것은 완전히 무의미합니다. 왜 OP가 그렇게 명백하게 잘못하도록 가르쳐 줍니까? – shmosel

관련 문제