![](/img/trans.png)
[英]trying to read from a file and split it into words and store those words in an arrayList
[英]Split ArrayList of sentences into words, store the words into new ArrayList
我已經做了一些研究,也許我只是在問正確的問題。
我有一個句子的ArrayList。 我想拆分句子並將單個單詞存儲到新的ArrayList中以供以后使用。 我已經包含了代碼。
這是我的第一篇文章。 如果有什么不對,我謹向您致歉。
僅供參考,rawLinesTest是我的句子ArrayList。
public List<String> getRawWords()
{
String[] tokens = rawLinesTest.toArray(new String[0]);
for(String s : tokens)
{
tokens = s.split("[^\\w]+");
rawWords = Arrays.asList(tokens);
}
return rawWords;
}
解:
public ArrayList<String> getRawWords(String s)
{
String[] s2 = s.split(" ");
ArrayList<String> words = new ArrayList<String>();
for(int x =0; x< s2.length-1;x++)
{
words.add(s2[x]);
}
return words;
}
要稍后使用,只需將arraylist分配給返回的列表。
String sentence = "A Test Sentence";
ArrayList<String> q = new ArrayList<String>();
q = getRawWords(sentence);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.