2011-12-04 83 views
1

好吧,我已經難倒了最好的方法來做到這一點,我寫了代碼從txt文件中讀取代碼行爲List。然後,我可以打印特定部分或將其轉換爲對象數組。但是,最終我想只有一個可以在C/C++中經常看到的2d int數組。談到java時,我非常青睞,只是本週早些時候纔開始。我一直喜歡它,直到在運行時創建動態二維數組爲止。你們中的任何一個人都可以提出一個從我當前卡住的地方進入2d int數組的好方法。我正要使用'toChar'將它轉換爲char數組,然後將(value @ index-48)存儲到它的相應位置,但對我來說這似乎很不起眼。列表<String> ----> int [] [] arr

====更新==========================

嗯,感謝所有的答覆,但我只是想出瞭如何使用雙打來做到這一點,所以對於其他人來說,在這裏你走了。我仍然寧願int,因爲我已經使用這種類型構建了我的其他matrixops類,但Double不應該是我猜測的問題。

package uaa.cse215; 
import java.io.*; 
import java.util.*; 


public class ReadMatrix { 
    private Double[][] A; 
    private Double[][] B; 
    private int count; 

    public int filedir(String matrix) throws Exception{ 
     Double[][] Temp; 
     String[] arr; 
     BufferedReader rd = new BufferedReader(new FileReader(matrix)); 
     String s; 
     List<String> textFile = new ArrayList<String>(); 
     while ((s=rd.readLine())!=null) { 
      textFile.add(s); 
      } 
     String splitarray[] = textFile.get(0).split(" ");//run once to grab # cols 
     int rows = textFile.size();//number of rows 
     int cols = splitarray.length;//number of cols 
     Temp = new Double[rows][cols]; // now can initiate array 
      for (int i=0; i<rows; i++) { 
       s = textFile.get(i); 
       arr = s.split(" "); 
       for (int j=0; j<cols; j++) { 
        Temp[i][j] = Double.parseDouble(arr[j]); 
       } 
      } 
      count++; 
      if (count == 1){ 
       A = Temp; 
      } 
      else 
       B = Temp; 


      rd.close(); 
      return(1); 
      } 
} 

回答

0

如果輸入矩陣dimentions是動態的或鋸齒狀的,你可以使用

List<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); 

閱讀數量和比它複製到原始二維數組,如果你想要的。

java.util。掃描儀具有從輸入讀「類型」數據很多方便的方法

下面是一個例子把文件讀入二維數組

public static int[][] read2DArray(String fileName) throws FileNotFoundException { 

     Scanner sc = null; 

     List<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); 

     int columnCount = 0; 
     int[][] arr = null; 

     try { 

      sc = new Scanner(new File(fileName)); 

      while (sc.hasNextLine()) { 

       // Read line 
       String line = sc.nextLine(); 

       // Split it 
       String[] nums = line.split(" "); 

       if (nums.length > columnCount) { 
        columnCount = nums.length; 
       } 

       // Convert to integers and add to list 
       list.add(new ArrayList<Integer>()); 
       for (String n : nums) { 
        list.get(list.size() - 1).add(new Integer(n)); 
       } 
      } 

      // Convert list to array 
      int rowCount = list.size(); 
      arr = new int[rowCount][columnCount]; 
      for (int i = 0; i < rowCount; i++) { 
       for (int j = 0; j < list.get(i).size(); j++) { 
        arr[i][j] = list.get(i).get(j); 
       } 
      } 

     } finally { 
      if (sc != null) { 
       sc.close(); 
      } 
     } 

     return arr; 
    } 
+0

你在這2個小時前在哪裏哈哈。我一直在尋找這樣的東西,並與評論!非常感謝 –

2

請注意,Java有char數據類型,是一個16位無符號整數拿着UTF-16碼點。 Java中的int始終是一個帶符號的32位整數。所以,如果你想要一個像表示字符串的內容字符數組一個C,你應該使用char[][]

要將List<String>的內容轉換成二維數組,你可以使用下面的代碼:

char[][] twoDarray = new char[textFile.size()]; 
for(int i = 0; i < textFile.size(); i+) 
{ 
    twoDarray[i] = textFile.get(i).toCharArray(); 
} 

數組twoDarray包含所有的字符串,每個字符串都是char數組。

+0

的空間將被放入數組 – GETah

+0

我沒有看到這是行不通的空間問題。如果原始字符串包含空格,則char [] []保留這些空格的行爲意味着,空格僅僅是一個特殊用途的字符。 –

+0

不,PO想要直接得到整數。解析空格字符將導致數字格式異常 – GETah

0

假設數據文件包含要解析成整數ASCII代表的數字:

11 -9 13 
12 55 102 
1 1 1024 

然後你可以使用Integer(String s)構造解析你的字符串對象。

此外,我建議分裂每一行只有一次。對於小型數組來說不會有太大影響,但是您的投入越大,您將越不必要地重新計算分組。

的(另)重寫:

int tmp[][] = new int [rows][cols]; 
for(int i=0;i<rows;i++){ 
    splitarray = textFile.get(i).split(" "); 
    for(int j=0;j<cols;j++){ 
     tmp[i][j] = Integer(splitarray[j]); 
    } 
} 
1

這條線將不會編譯

splitarray[j] = textFile.get(i).split(" "); 

作爲splitarray[j]String類型和split的返回的數組String小號

請改爲:

for(int row=0;row<textFile.size();row++){ 
     String[] splitarray = textFile.get(row).split(" "); 
     for(int col=0;col<splitarray.length;col++){ 
      tmp[row][col] = Integer.parse(splitarray[col]); 
     } 
    } 
+0

是的,我看到了錯誤,我將嘗試解析方法,但我只看到它與對象數組完成,所以不知道該怎麼做。謝謝 –