2014-02-18 196 views
0

我試圖將2維字符串數組複製到另一個2維字符串數組。然後將第二個2維字符串數組轉換爲字符串。之後,打印出字符串 ,但字符串不顯示什麼是假設... 我試圖做到這一點,而不使用Java庫。Java將2維字符串數組轉換爲字符串

public class Train { 
//public static void main (String args){ 
public static void main(String[] args) { 
String [][] ff = { {"0","1","2","3","4","5","6","7"}, 
        {"0","1","2","3","4","5","6","7"}, 
        {"0","1","2","3","4","5","6","7"}, 
        {"0","1","2","3","4","5","6","7"}, 

        {"0","1","2","3","4","5","6","7"}, 
        {"0","1","2","3","4","5","6","7"}, 
        {"0","1","2","3","4","5","6","7"}, 
        {"0","1","2","3","4","5","6","7"} }; 


    String[][] IP = new String[8][8]; 
    int x=0, y=0; 
     for(int i = 1; i < 8; i +=2){ 

      for(int j = 7; j>=0 ; j--){ 
       y= 0; 
      IP[x][y]=ff[j][i]; 
      System.out.print(IP[x][y]); 
        y++;    
      } 
     if (i==7){ 
      i=-2; 
      x++; 
      } 

     } 
       String zz= ""; 
    for(int p = 0; p<8;p++){ 
     for(int q= 0 ; q< 8; q++){ 
     zz += IP[p][q]; 
     System.out.print(zz); 
     } 
    } 
    } 

} 

謝謝

+0

什麼是你的問題?你喲有錯誤嗎? – 2014-02-18 07:41:14

+0

您正在將元素從ff [] []始終複製到IP [0] [0](x,y永遠不會更改)。 – wxyz

+0

我得到這個[[Ljava.lang.String; @ 134510ac – user3322264

回答

0

使用此:String yourString = Arrays.toString(yourArray);

+0

感謝您的時間,但正如我所說我不想使用圖書館 – user3322264

+0

此外,這並沒有工作 – user3322264

0
System.out.print(IP); 

在Java中,每個對象都有toString()方法,默認是顯示類的名稱表示,再加入@然後哈希碼

你應該使用Arrays#deepToString

System.out.println(Arrays.deepToString(yourArray));

+0

正如我說我不想使用圖書館。然而,我試過了,它給了我一些空值 – user3322264

+0

謝謝你:) – user3322264

+0

你可以迭代數組,有兩個循環。 – Maroun

0

System.out.print(IP); 

只不過想打印出對象表示。當你創建一個zz字符串時,你幾乎就在那裏。嘗試在迭代後打印出來。

因爲您不想使用任何Java庫,您將需要重新遍歷數組。當然,如果您認爲java.util.*是一個庫。

0

如果你不想使用的庫:

String ffStr = ""; 
for (String[] sArray : ff) { 
    for (String s : sArray) { 
     ffStr += s; 
    } 
} 
System.out.println(ffStr); 
+0

我很感謝你們幫助西蒙。現在,它打印出陣列ff罰款;然而,當我嘗試打印出陣列IP,它複製了陣列ff時,它給了我所有值的空值 – user3322264

+0

你是否想要不使用包含在數組中的方法呢? – SimonPJ