2017-02-11 40 views
-1

我有這個字符串"5,7,6,1",我想使大小爲4的整數數組是這樣的:out={5 7 6 1}out[0]=5out[1]=7和... 其實我想有串的數目一個整數數組,但是當我打印out輸出數組out這樣的地址:[[email protected] 什麼不對的代碼????整數數組工作在java中

public static void main(String[] args) { 
     String a = new String(); 
     a="5,7,6,1"; 
     int element, temp = 0, ans = 0,j=0; 
     element = a.length()/2 + 1; 
     int[] out = new int[element]; 
     for (int i = 0; i < a.length(); i++) { 
      if (j < element) { 
       temp = (int) a.charAt(i); 
       if (temp != 44) { 
        ans = ans * 10 + temp; 
       } else { 
        out[j] = ans-48; 
        ans=0; 
        j++; 
       } 
       if(i==a.length()-1){ 
        out[j] = ans-48; 
       } 
      } 
     } 
     int[] array=new int[element]; 
     System.out.println(out); 
    } 
+0

採用分體式 - a.split( 「」) – Janar

+0

的String []將intArr =一個。分裂(」,」); int [] n1 = new int [intarr.length]; 對(INT N = 0; N jophab

+0

它再次顯示相同的結果,對於intarr和n1它都顯示變量的地址! – rahele

回答

0

我認爲你可以使用:

Integer[] out = new Integer[element]; 

然後打印這樣的:

System.out.println(Arrays.asList(out)); 
+0

TNX,是的,它顯示出數組的所有元素,但我想這樣做陣列上的一些操作和我所有的其他參數INT,這是不是可以硬用這個參數來工作? :( – rahele

+0

我認爲你可以使用Integer作爲使用int的相同方式。 – algojava

+0

非常感謝你:) – rahele