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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從文本文件創(chuàng)建的字典 - contains() 總是返回 false

從文本文件創(chuàng)建的字典 - contains() 總是返回 false

藍(lán)山帝景 2021-11-03 09:57:51
我目前正忙于一項(xiàng)小型大學(xué)作業(yè),并且在使用我實(shí)現(xiàn)的字典類的 contains() 方法時(shí)遇到了一些問(wèn)題 - 該方法總是返回 false。這個(gè)類看起來(lái)像這樣:public class LocalDictionary {    private ArrayList<String> wordsSet;    public LocalDictionary() throws IOException {        String wordListContents = new String(Files.readAllBytes(Paths.get("words.txt")));        wordsSet = new ArrayList<>();        String[] words = wordListContents.split("\n");        for (int i = 0; i < words.length; i++) {            wordsSet.add(words[i].toLowerCase());        }    }    public boolean contains(String word) {        return wordsSet.contains(word.toLowerCase());    }}字典從中獲取單詞的“words.txt”文件可在https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt獲得,但這里是它的外觀片段:zinkedzinkenitezinkyzinkiferouszinkifyzinkifiedzinkifieszinkifyingzinniazinniaszinnwalditezinoberzinsangzinzarzinziberaceae我已經(jīng)確?!皐ords.txt”中的單詞包含在“wordsSet”中,但無(wú)法弄清楚為什么 contains 方法對(duì)于似乎在 ArrayList 中的單詞返回 false。非常感謝任何幫助。
查看完整描述

2 回答

?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

嘗試BufferedReader,我嘗試并為我工作(我刪除了一些無(wú)用的行)。在您的使用中,您從文件中讀取所有字節(jié),會(huì)有額外的字節(jié)。


public class LocalDictionary {

    private ArrayList<String> wordsSet = new ArrayList<>();


    public LocalDictionary() throws Exception {


        //dont forget to absolute path to here. click righ click to file and copy path

        File file = new File("C:\\Users\\higuys\\IdeaProjects\\try\\src\\words.txt");

        BufferedReader br = new BufferedReader(new FileReader(file));


        String line;

        while ((line = br.readLine()) != null)

            //trim and tolowercase and add to list.

            wordsSet.add(line.trim().toLowerCase());


    }


    public boolean contains(String word) {

        return wordsSet.contains(word.toLowerCase());

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-11-03
?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

嘗試BufferedReader,我嘗試并為我工作(我刪除了一些無(wú)用的行)。在您的使用中,您從文件中讀取所有字節(jié),會(huì)有額外的字節(jié)。


public class LocalDictionary {

    private ArrayList<String> wordsSet = new ArrayList<>();


    public LocalDictionary() throws Exception {


        //dont forget to absolute path to here. click righ click to file and copy path

        File file = new File("C:\\Users\\higuys\\IdeaProjects\\try\\src\\words.txt");

        BufferedReader br = new BufferedReader(new FileReader(file));


        String line;

        while ((line = br.readLine()) != null)

            //trim and tolowercase and add to list.

            wordsSet.add(line.trim().toLowerCase());


    }


    public boolean contains(String word) {

        return wordsSet.contains(word.toLowerCase());

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-11-03
  • 2 回答
  • 0 關(guān)注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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