簡體   English   中英

當我將其輸入到另一個arrayList中時,如何修改我的arrayList,以便刪除並更改它的值?

[英]How do I modify my arrayList as I enter it into another arrayList such that to remove and change it's values?

我創建了一個數組列表,用於存儲文本文件中的單詞。 但是現在我需要創建一個單獨的數組列表,只接收文件中單詞的字母將它們恢復為小寫並刪除單詞或其周圍的所有標點符號。 所以基本上它會恢復所有提到的所有元素的單詞。

        List<String> grams = new ArrayList<String>();
        for(String gram : words){
            gram = gram.trim();
            for(int i=0,int l=gram.size();i<l;++i){ //this line is wrong
                const String punChars = ",[]:'-!_().?\/~"; //this line is wrong
                if(gram.indexOf(i) != -1){ //this line is wrong
                    gram.remove(i); //this line is wrong
                }
                gram.add(gram.remove(0).toLowerCase()); //this line is wrong
            }
        }

我基本上嘗試讀取數組中的選擇字符串中的每個字符,因為我將它放入新的數組列表中然后如果它有任何標點符號或其中我刪除它:我嘗試使用const來存儲它標點值然后我用if語句檢查字符串以刪除字符串中的該位置。

接下來我嘗試添加單詞但刪除大寫並將其更改為小寫。

我有點失落,不知道我在這里做了什么......

我對你的問題的解釋是你想要查看單詞列表,將它們轉換為小寫,刪除標點符號,然后將它們插入到另一個列表中? 如果是這種情況,那么我不明白為什么你需要修改原始列表。 你可以這樣做:

for(String gram : words) {
    gram = gram.trim(); //trim string
    gram = gram.replaceAll("[^A-Za-z0-9]", ""); //remove any non alphanumeric characters
    grams.add(gram); //add to the grams list
}

為什么不循環遍歷String中的每個字符,如下所示:

for (char c : gram.toCharArray()) {
  ...
}

然后檢查你的角色是否在被排除的字符列表中,如果沒有,則將其小寫並將其添加到輸出列表中?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM