第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

令牌 - 字符串 Java

令牌 - 字符串 Java

SMILET 2021-12-30 20:31:00
我有以下任務(wù)我成功了,但是代碼效率很低,如果有人能告訴我一種更有效的方法,也許是子字符串,我將不勝感激。那作業(yè):/** * Separates a given string into tokens, which are the "words" that are * separated by one or more occurrences of the given separator character. * Returns the tokens as an array of String values. */public static String[] tokenize (String str, char separator) {    // Removes all the occurrences of the separator at the beginning and end of str    String source = trim(str, separator);    String[] tokens = new String[charRunCount (source,separator)+1];    String tmp = ""; // a string in order to take a word, then run over this string    int j = 0;    int i = 0;    while (i < tokens.length) {        if ( source.charAt (j) != separator ) {            do {                tmp += source.charAt (j);                if ( j >= source.length () - 1 ) {                    break;                }                else { // so that we math the source length                    j++;                }            } while (source.charAt (j) != separator);         }        if ( source.charAt (j) == separator ) {            j++;            while (source.charAt (j) == separator) {                j++;            }        }        tokens[i] = tmp;// taking the token into place        tmp = ""; //resetting the token so we can begin anew        i++;    }    return tokens;}cahrRunCount 函數(shù):    public static int charRunCount(String str, char c){    char last = 0;    int counter = 0;    for (int i = 0; i < str.length(); i++) {        // whenever a run starts.        if (last != c && str.charAt(i) == c) {            counter++;        }        last = str.charAt(i);    }    return counter;}我不能使用 import 或 regex,謝謝!
查看完整描述

2 回答

?
慕妹3146593

TA貢獻1820條經(jīng)驗 獲得超9個贊

使用 String.split 方法


String[] tokens = str.split(separator)



for(String token:tokens){

//your code goes here

}

文檔在這里


https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)


查看完整回答
反對 回復(fù) 2021-12-30
?
猛跑小豬

TA貢獻1858條經(jīng)驗 獲得超8個贊

如果需要,您可以使用 String 類的 split 方法(就像@Amidala Siva Kumar 建議的那樣),如下所示:


public static String[] tokenize (String str, char separator) {

    String[] tokens = str.split(separator);

    return tokens;

}

或者,如果您想使用自己的拆分來執(zhí)行此操作,則可以這樣做(對代碼的改進)。


public static String[] tokenize (String str, char separator) {

    String sep = ""+separator;

    int max_size = str.length() - str.replace(sep, "").length() +1 ; // Calculate max array size

    String[] temp = new String[max_size];

    int start = 0, index = 0, exact_size = 0;

    int pos = str.indexOf(separator);

    while (pos>=start) {

        if (pos>start){

            temp[index++] = str.substring(start,pos).trim();

            exact_size++;

        }

        start = pos + 1;

        pos = str.indexOf(separator,start); 

    }

    String[] tokens = new String[exact_size];

    System.arraycopy(temp, 0, tokens, 0, exact_size); 

    return tokens;

}

希望你覺得它有用。


查看完整回答
反對 回復(fù) 2021-12-30
  • 2 回答
  • 0 關(guān)注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號