2014-12-07 65 views
0

我不能使用來自任何類的方法,除了字符串和IO類值轉換成一個整數數組轉換字符串元素爲

所以我的代碼段是:

String line = reader.readLine(); 
while (line != null) { 
    String[] elements = line.split(","); 
    // Array could be too big if there are multiple occurances of 
    // the same number 
    // Array length + 1 because I can't use the 0 and with a input line of 
    // 1,2,3 for example would be the length 3 but I would have the 
    // numbers 0,1,2 in the Array as my index. 
    String[][] placeholderMatrix = new String[elements.length+1][elements.length+1]; 
    for(int i = 0; i < elements.length-1; i++){ 
     placeholderMatrix[(int)elements[i]][(int)elements[i+1]] = 1; 
    } 
    line = reader.readLine(); 
} 

在我得到的文件是唯一的號碼,如認爲:1,2,3,4,5,8,7,4 所以在我的分裂字符串數組只是數字,但現在如果我想使用它們作爲我的索引我矩陣(佔位矩陣)

我的問題是在我的循環,我想用它們我的索引我不能使用它們,因爲它是一個字符串數組。通常我會使用Integer.parseInt,但我不允許:/ 關於如何將它們實現爲我的索引的任何想法?和任何想法如何我可以得到我矩陣的完美長度?因爲如果我得到以下數字:1,2,2,2,3我的矩陣應該只有數字:

0 1 2 3 
1 
2 
3 

但如果我使用elements.length + 1我矩陣的長度我將得到的數字0 1 2 3 4 5

希望你能理解我的問題。對不起,我的英語不好,並提前致謝。

編輯:所以我得到了另一個問題。如果我實施帝賜的方法(parseInt函數),並在線路使用它AM 「placeholderMatrix [解析(元素[I])] [解析(元素[I + 1])] = 1;」我得到了錯誤ArrayOutOfBounce,因爲我定義的Array只是我分割的String Array元素的長度。但是如果我用Integer.MAX_VALUE將其定義爲我的長度,則會因內存太大而導致內存錯誤。有任何想法嗎?

編輯2:我的任務: 我必須拿一排由「,」分隔的數字。 (我將它與字符串分割方法拆分得到的只是數字)現在我要創建一個矩陣(2維陣列)和我的新字符串數組的索引我和索引i的數量在尋找數+ 1,並且必須將第一個Number作爲我的列,第二個作爲我的列(反之亦然),並在該點實現1.現在我的Numbers將從1到Integer.MAX_VALUE,因此我必須創建這樣的一個大矩陣,但這是不可能的,因爲我得到了MemoryError。

錯誤:java.lang.OutOfMemoryError:要求數組大小超過VM限制 在Test.main(Test.java:29)

明白我必須做的:http://de.wikipedia.org/wiki/Adjazenzmatrix圖像在正確的但數字從Integer.MAX_VALUE,所以我的二維數組必須定義長度爲Integer.MAX_VALUE?

編輯:

所以帝賜要求的例子:

我的順序可能是:1,2,5,4 所以我的矩陣應該是:

sample matrix

希望這是你想要的Dici 但是我能從序列中得到的數字是1到Integer.MAX_VALUE

+0

您正在尋找一種手動方式來執行'Integer.parseInt'? – 2014-12-07 10:46:42

+0

以及我認爲這將解決問題 – NhatNienne 2014-12-07 10:48:07

+0

也許你可以使用'15'爲'10 * 1 + 5'的事實。另一種可能性,如果你只有在[0-9]中的值是使用字符減法(即字符值 - '0') – 2014-12-07 10:53:01

回答

0

爲了將字符串轉換爲整數,你可以簡單地實現你自己的整數分析器,它並不複雜。您可以從此開始並在需要時改進它。

public int parseInt(String s) { 
    int n = 0; 
    int pow = 1; 
    for (int i=s.length() - 1 ; i>=0 ; i--) { 
     String si = String.valueOf(s.charAt(i); 

     if (si.matches("[0-9]")) { 
      n += pow*(s.charAt(i) - '0'); 
      pow *= 10; 
     } else if (si.matches("+|-") && i == 0) 
      n *= s.charAt(i) == '+' ? 1 : -1; 
     else 
      throw new NumberFormatException();   
    } 
    return n; 
} 

然後,我會處理您的問題的第二部分。如果Integer.MAX_VALuE是您的輸入值之一,則不可能分配Integer.MAX_VALUE x Integer.MAX_VALUE矩陣。您需要做的是爲您的輸入值分配連續的ID並在地圖中記錄ID,以便您可以輕鬆訪問對應於一個節點值的矩陣索引。這裏有一個例子讓你明白:

public void someMethod() { 
    int id = 0; 
    Map<Integer,Integer> idMap = new HashMap<>(); 
    String[] split = reader.readLine().split(","); 
    int [] nodes = new int[split.length]; 

    for (int i=0 ; i<nodes.length ; i++) { 
     nodes[i] = parseInt(split[i]); 
     if (!idMap.containsKey(nodes[i])) 
      idMap.put(nodes[i],id++); 
    } 

    // the map is now constructed, it should probably be stored in an attribute 

    int[][] placeholderMatrix = new int[nodes.length][nodes.length]; 
    for(int i = 0; i < nodes.length; i++){ 
     if (i > 0)    placeholderMatrix[idMap.get(nodes[i])][idMap.get(nodes[i-1])] = 1; 
     if (i < nodes.length-1) placeholderMatrix[idMap.get(nodes[i])][idMap.get(nodes[i+1])] = 1; 
    } 
} 

還有其他的方法來做到這一點,讓我知道,如果這個解決方案是確定

+0

我的值來自[1; Integer.MAX_VALUE]; – NhatNienne 2014-12-07 10:56:11

+0

那又如何?只要s代表有效的int,此代碼就會返回正確的值。 – Dici 2014-12-07 10:59:43

+0

它呢?我很困惑你的行如果檢查字符串是否匹配[0-9] 那麼charAt正在檢查索引i中的數字是否爲[0-9]? – NhatNienne 2014-12-07 11:00:28

0

你可以這樣做:

String keyword = "1,2,3,4,5,8,7,4";//input line from file 
    String replacedKeyword = keyword.replaceAll("[^\\d]", "");//except numbers replace all. Assuming one digit numbers only. 
    String[][] placeholderMatrix = new String[replacedKeyword.length()+1][replacedKeyword.length()+1]; 
    char keys[] = replacedKeyword.toCharArray(); 
    for (int i = 0; i<keys.length - 1; i++) { 
    placeholderMatrix[keys[i] - '0'][keys[i + 1] -'0'] = "1"; 
    } 
+0

作品我可以在幾行代碼,但在這可重用解決方案 – Dici 2014-12-07 11:09:35

+0

絕對,但如果你只是允許使用字符串和IO常規和OP沒有提及約兩位數字,因此假設。 – SMA 2014-12-07 11:17:17

+0

在你的第二行我應該只能使用.split(「,」);因爲我知道我的號碼僅由分隔「」 – NhatNienne 2014-12-07 11:33:45

0

我真的不明白你想要什麼。但是,如果這將幫助一個簡單的方法將字符串數字轉換爲int:

int toInt(String number) { 
    int num = 0; 
    for (int i=0; i<number.length(); i++) { 
     num = num*10 + (number.charAt(i)-'0'); 
    } 
    return num; 
} 
+0

非常感謝,但我已經有了這樣一種方法。我目前的問題是,我的Matrix我嘗試創建的內存太大,您是否有空單元格 – NhatNienne 2014-12-07 18:35:28

+0

? – hasan83 2014-12-07 18:36:02

+0

是的最有可能的,但它也可能是,但如果我的教授正在與所有細胞龐大的線被稱爲那麼沒有笑..你有一個想法如何減少空單元格了呢? – NhatNienne 2014-12-07 20:12:19