2014-09-27 3 views
-5

정수를 Integer 개체로 변환하는 convert2ObjAndStore 함수가 포함 된 WrapperDemo 클래스를 만들고이 Integer 개체를 intVector.Store "n "intVector.Iterate 벡터와 정수 값으로 객체를 받고 그것을 표시합니다. 또한 하나의 Integer 객체를 다른 객체와 교환하고 호출 된 루틴에 교환 된 값을 표시하는 함수를 포함합니다 (주 함수는 WrapperMain)래퍼 클래스 (프리미티브 배열을 래퍼 배열로 변환하는 방법

나는이 시도했지만 정수 개체를 interger 변환 오류가 발생했습니다

import java.util.*; 

class WrapperDemo { 

    static void Convert(Integer []ob,int n) { 

     Vector<Integer> store=new Vector<Integer>(); 

     for(int i=0;i<n;i++) { 
      store.add(ob[i]); 
      System.out.println(store.get(i)); 
     } 
    } 
} 

class WrapperMain { 

    public static void main(String[] args) { 

     Scanner sc=new Scanner(System.in); 
     int n=sc.nextInt(); 
     sc.nextLine(); 

     int[] x=new int [20]; 

     for(int i=0;i<n;i++) { 
      x[i]=sc.nextInt(); 
      WrapperDemo.Convert(x,n); 
     } 
    } 
} 
+1

서식을 수정하십시오. –

+0

* 오류가 발생합니다 * : 오류 메시지를 읽고 무엇이 잘못되었는지 그리고 어디에 있는지 확인합니다. –

답변

0

는 최대한 멀리 볼 수있는 당신의 오류는 int[]x를 선언하지만 Integer[]를 기대하는 방법에 전달한다는 것입니다. 이것은 autoboxed되지 않습니다!

편집 : 그것은 명확하게하려면 ...

class WrapperDemo { 

static void Convert(Integer []ob,int n) { // <-- type Integer[] 

int[] x=new int [20]; // <-- type int[] which is NOT equal to Integer[] 
         //  and will NOT be autoboxed to Integer[] 
// ... 
WrapperDemo.Convert(x,n); // You give type int[] to a method that is expecting Integer[] 

대 그리고 사이드 노트로 :

for(int i=0;i<n;i++) { 
     x[i]=sc.nextInt(); 
     WrapperDemo.Convert(x,n); // <- You call the convertion in each iteration. 
    } 

당신의 각 반복에 convertion 전화 필 루프. 그래서 대부분의 항목은 null이됩니다. 다음과 같이 변경하십시오.

for(int i=0;i<n;i++) { 
     x[i]=sc.nextInt(); 
} 
WrapperDemo.Convert(x,n); 
+0

나는 Interger를 'Integer []'ob로 선언했다. –

+0

네, 그렇지만'int []' 타입의'x'로 호출합니다. – Fildor

+0

U 형식 매개 변수에서 그 말은 'Integer [] ob'대신 'int [] ob'을 씁니다. –