2013-01-24 73 views
1

我有從字符串轉換爲二維詮釋數組的問題。
比方說,我有:(在我的計劃是將字符串從文本區)將字符串轉換爲二維詮釋數組

String x = "1,2,3;4,5,6;7,8,9" 

,我想創建數組n x n

int[3][3] y = {{1,2,3},{4,5,6},{7,8,9}} 

我嘗試(必要時作下一階段。)分割字符串並創建1維數組,但我沒有任何好的想法接下來要做什麼。


正如你建議我嘗試先用;然後,分裂,但我的解決方案都不是很大。它僅在將有表時纔有效。如何創建一個循環制作字符串數組?

public int[][] RunMSTFromTextFile(JTextArea ta) 
    { 
     String p = ta.getText(); 
     String[] tp = p.split(";"); 

     String tpA[] = tp[0].split(","); 
     String tpB[] = tp[1].split(","); 
     String tpC[] = tp[2].split(","); 

     String tpD[][] = {tpA, tpB, tpC}; 

     int matrix[][] = new int[tpD.length][tpD.length]; 

     for(int i=0;i<tpD.length;i++) 
     { 
      for(int j=0;j<tpD.length;j++) 
      { 
       matrix[i][j] = Integer.parseInt(tpD[i][j]); 
      } 
     } 
     return matrix; 
    } 
+0

您必須使用split by;然後對數組中的每個條目進行拆分, – Miquel

+0

如您所說,使用'split'聽起來像是一個可以進一步發展的想法。 – madth3

+0

當我僅使用逗號爲劈裂:「\t公共INT [] RunMSTFromTextFile(JTextArea中TA) \t { \t \t字符串P = ta.getText(); \t \t String [] tp = p.split(「,」); \t \t int matrix [] = new int [tp.length]; \t \t對(INT I = 0; I user2009164

回答

2

使用拆分後,請看Integer.parseInt()以獲取數字。

String lines[] = input.split(";"); 
int width = lines.length; 
String cells[] = lines[0].split(","); 
int height = cells.length; 
int output[][] = new int[width][height]; 

for (int i=0; i<width; i++) { 
    String cells[] = lines[i].split(","); 
    for(int j=0; j<height; j++) { 
     cells[i][j] = Integer.parseInt(cells[j]); 
    } 
} 

然後,你需要決定如何通過;NumberFormatExceptions

+0

+1比我更快(&更好)。 ;) –

+0

我相信你的意思是說字符串單元格[]。 – 2013-01-24 23:30:57

+0

@傳奇固定。謝謝。 –

2

如果您已經創建了一個int[9]並希望將其分割成int[3][3]

for (int i = 0; i < 3; i++) { 
    for (int j = 0; j < 3; j++) { 
    toArray[i][j] = fromArray[(3*i) + j); 
    } 
} 

現在,如果二維陣列不是矩形,即內部數組的大小是不一樣對於所有外部陣列,那麼你需要更多的工作。您最好使用Scanner並在nextStringnext之間切換。最大的挑戰將是,直到您到達行終止分號時,您纔會知道每行中元素(列)的數量。

3
  1. 斯普利特做才能行。
  2. 環它們,通過,遞增計數器(例如x
    1. 拆分獲得的每行的值。
    2. 環路那些值,遞增計數器(例如y
      • 解析每個值(例如,使用中的IntegerparseInt方法之一),並將它添加到陣列的x,y
+3

+1領先不喂勺。可能應該包含解析方法的名稱。 –

+0

@MelNicholson好點,完成。 –

0

使用2個分割一個解決方案:

String input = "1,2,3;4,5,6;7,8,9"; 

String[] x = input.split(";"); 

String[][] result = new String[x.length][]; 
for (int i = 0; i<x.length; i++) { 
    result[i] = x[i].split(","); 
} 

這給你需要事後分析的整數串的2維數組,這取決於你想要使用這些數字。以下解決方案顯示如何在構建結果時解析它們:

String input = "1,2,3;4,5,6;7,8,9"; 

String[] x = input.split(";"); 

int[][] result = new int[x.length][]; 

for (int i = 0; i < x.length; i++) { 
    String[] row = x[i].split(","); 
    result[i] = new int[row.length]; 

    for(int j=0; j < row.length; j++) { 
     result[i][j] = Integer.parseInt(row[j]); 
    } 
} 
0

超級簡單的方法!

package ADVANCED; 
import java.util.Arrays; 
import java.util.Scanner; 

public class p9 { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Scanner sc=new Scanner(System.in); 

     String x=sc.nextLine(); 
     String[] array = x.split(","); 
     int length_x=array.length; 
     int[][] two=new int[length_x/2][2]; 

     for (int i = 0; i <= length_x-1; i=i+2) { 
      two[i/2][0] = Integer.parseInt(array[i]); 
     } 

     for (int i = 1; i <= length_x-1; i=i+2) { 
      two[i/2][1] = Integer.parseInt(array[i]); 
     } 
    } 
}