搜尋
首頁Java如何將字串標記儲存到陣列中

php小編草莓將為大家介紹如何將字串標記儲存到陣列中。在程式設計過程中,我們經常需要處理字串並將其拆分成多個標記。將這些標記儲存到數組中可以方便我們處理和操作。本文將詳細說明如何使用php中的函數和方法來實現這項功能,幫助讀者更好地理解和運用。無論您是初學者還是有一定經驗的開發者,都可以從本文中獲得有益的知識和實用的技巧。讓我們一起開始吧!

問題內容

我參考了一些範例來成功提取使用者輸入的每個部分。但只能提取1次。應該有 2 個循環用於提取多個輸入並將標記保存在數組中。我被困在陣列上,我該怎麼辦?

question:
write a program that accepts string tokens in the format of token1:token2:token3:token4 , where : is used as delimiter to separate tokens. there should be two functions, ingest and appearance.

ingest takes a string, and stores it in the collection.

appearance takes a string as input . it returns a normalized value
between 0 to 1,  where the value represents the percentage of
appearances of stored tokens which have input as the prefix.

state the space and time complexity of your solution.


預期結果:

ingest('mcdonal:uk:employeea')
ingest('mcdonal:hk:employeea')
ingest('mcdonal:hk:employeeb')
ingest('mcdonal:hk:employeec')
ingest('fastfood')

appearance('mcdonal')
# > 0.8
appearance('mcdonal:hk')
# > 0.6

我的程式碼:

String input;
   
        
 // For user input
 Scanner sentense = new Scanner(System.in);
 input = sentense.nextLine();   
 String[] ingestWords = {};
       
             
 // Use ':' to seperate input
 StringTokenizer st = new StringTokenizer(input, ":");
 while(st.hasMoreTokens()) { 
   System.out.println(st.nextToken());
 }

解決方法

我建議您分兩步驟解決您的問題。

首先,攝取部分:您需要接受使用者輸入的單字,並將每個單字儲存在arraylist<string></string> 中,而不是固定大小的陣列中,因為您事先不知道將獲得多少個輸入。

範例程式碼如下所示。

public static void main(string[] args) {
    string input;
    list<string> ingestwords = new arraylist<>();

    scanner sentence = new scanner(system.in);
        
    while (sentence.hasnext()) {
        input = sentence.next();
            
        if (input.equals("exit")) { // to stop receiving input
            break;
        }
            
        ingestwords.add(input);         
    }
    sentence.close();
}

第二,外觀部分:給定一個字串,您需要從攝取部分迭代單字列表,並檢查哪些單字以給定字串開頭。

例如,您可以建立一個像這樣的輔助函數。

void hasPrefix(String word, String str) {
    return word.startsWith(str);
}

將此函數應用於 ingestwords 中的每個單詞,將為您提供以 str 作為前綴的單字數。並且你可以從中算出出現的百分比。

以上是如何將字串標記儲存到陣列中的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:stackoverflow。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SecLists

SecLists

SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能