2016-02-19 28 views
-4

我必須聲明一個字符數組,並且必須創建兩個函數,即:接受字符集及其檢查字符串位置數組並返回第一個出現位置的函數。查找字符數組中的字符位置

返回該字符的最後一個indx的第二個函數。

+1

所以什麼問題了嗎? – Satya

+1

您可以將數組轉換爲字符串。 'String s = new String(arr);'。然後使用這些方法。 – Hackerdarshi

回答

2

類ExCharArray {

char數組[] = { 'S', 'X', 'Z', 'Y', 's' 的};

公共靜態無效的主要(字符串ARGS []){

 locateIndex('s'); 
    getLastLocation('s'); 

}

公衆詮釋locateIndex(焦CH1){

for (int i = 0; i < array.length; i++) { 

     if (array[i]==ch1){ 

      return i; 

     } 
    } 
    return -1; 
} 


public int getLastLocation(char ch2){ 


    for (int i = array.length-1; i>=0; i--) { 

     if (array[i]==ch2){ 

      return i; 

     } 
    } 

    return -1; 
} 


     for (int i = ch.length-1; i>=0; i--) { 

      if (ch[i]==chart){ 

       return i; 

      } 
     } 

     return -1; 
    }