2011-02-26 126 views
3

我想在java中編寫一個小方法,但我無法弄清楚。我希望能夠做的就是輸入一個字符串,然後一個int變量的值設置爲這個陣列中的指數,也就是說,如果我有包括在字符串數組中搜索子字符串?

[0] 'hi guys' 
[1] 'this' 
[2] 'is' 
[3] 'sparta' 

的數值數組我的整數設置爲0,我想找到第一個出現的「ta」,這將是[3],所以我希望函數將我的整數設置爲3.

我現在擁有的是完全脫離牆壁和錯誤,是否有任何簡單的方法來做到這一點?我已經有一個名爲get()的函數定義了返回當前行的值(即get(0)在這種情況下會返回'hi guys')。任何人都可以幫我嗎?

感謝很多:)

public void find(String line) { 
    boolean found = false; 
    int i = cursor + 1; 
    while (found = false && i!=cursor) { 
    if ((doc.get(cursor).indexOf(line) > 0)){ 
    cursor = i; 
    found = true; 
    }else { 
    cursor++; 
    cursor%=doc.size(); 
    i++; 

    } 
} 
} 
+0

這功課呢?如果是這樣,你應該這樣標記它。 – Wipqozn 2011-02-26 15:31:46

+0

這是功課嗎? – 2011-02-26 15:32:16

+0

@steveom:你絕對是在這裏的雜草,但爲了幫助你,我們需要一些信息:來自你想要放入數組和搜索的文本數據在哪裏?什麼類型的對象是doc現在?你正在嘗試閱讀一個文件嗎? – 2011-02-26 15:36:06

回答

1

如果正確地理解你的任務,我會做這樣的事情:

public int find(String line, int startPosition) { 
    if (doc[startPosition].contains(line) { 
     return startPosition; 
    } 
    for (int i = 0; i < Math.max(doc.size() - startPosition, startPosition); i++) { 
     if (startPosition - i > 0 && doc[startPosition - i].contains(line)) { 
      return startPosition - i; 
     } 
     if (startPosition + i < doc.size() && doc[startPosition + i].contains(line)) { 
      return startPosition + i; 
     } 

    } 
    return -1; 
} 

這將包含爲行參數傳遞的字符串數組中返回的第一個元素的索引。

+0

這可能非常接近,但我認爲OP需要在每行上進行子字符串搜索:從問題'找到第一個出現的「ta」,它是第三個示例條目的子字符串。 – 2011-02-26 15:40:13

+0

是的,這是正確的,保羅。我需要搜索每一行的子字符串。我遇到的主要問題是我有一個值,我們稱它爲i,它指向數組,並且可能指向數組中間的位置,我想遍歷數組並找到最近的索引到我,到子字符串存在的數組中。對不起,以前我說不清楚! – steveom 2011-02-26 16:04:48

+0

好的,我上面實現的是搜索一個子字符串,所以我不確定Paul在說什麼。現在它返回數組中的_first_索引,該索引對應於包含作爲參數傳遞的子字符串的字符串,而似乎應該有另一個參數,並且索引應該是與該參數值最接近的那個。在這種情況下,算法需要調整,我將適當地編輯我的原始代碼。 – 2011-02-26 16:10:35

2

通常我不這樣做,但今天是星期六,我很高興,並可能將喝醉

public void find(String line) { 
    boolean found = false; 
    int i = 0;; 
    while (i < doc.size()) { 
    if ((doc.get(i).indexOf(line) > 0)){ 
     cursor = i; 
     found = true; 
     break; 
    }else { 
     i++; 
    } 
    } 
    if (found) { 
     // print cursor or do whatever 
    } 
} 
0

我就不會是更理智搜索實際的字符串[]而不是每一行?

然後循環遍歷數組並返回當前索引,如果此位置的字符串包含子字符串。

2

你應該注意這是否是家庭作業。要做到這一點

一種方法是:

int i = 0; 
    String searchTerm = "ta"; 

    System.out.println("Following substrings contain search term:"); 
    for (String s : "hi guys,this,is,sparta".split(",")) { 
     if (s.contains(searchTerm)) System.out.println(i++); 
     else i++; 
    } 

或者如果你喜歡使用正則表達式,然後換用s.contains(searchTerm)s.matches(searchTerm)

如果這不是家庭作業,但面試問題或工作問題,這將是非常複雜的。例如:氨基酸序列是搜索詞,需要找到它所在的DNA/RNA位置。那樣的話你需要更復雜的解決方案。

實例:

1

他說,這不是功課,所以在這裏它是:

(此其實編譯和工作)

import java.io.*; 

    public class A { 
      public static void main(String[] args) { 
        String[] arr = {"hi guys", "this", "is", "sparta"}; 
        System.out.println("enter substring:"); 
        String substr = ""; 
        try { 
        substr = new BufferedReader(new InputStreamReader(System.in)).readLine(); 
        } catch(IOException e) {System.exit(0);} 
        for(int i =0; i<arr.length; i++) { 
          int charPos = arr[i].indexOf(substr); 
          if(charPos!=-1) { 
            System.out.println("found in string index " + i + " at "+charPos); 
            break; 
          } 
        } 
      } 
    }