亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何通過搜索給定字符并創建新的 ArrayList 來分解 ArrayList<String>?

如何通過搜索給定字符并創建新的 ArrayList 來分解 ArrayList<String>?

回首憶惘然 2023-10-19 21:52:07
我試圖通過在每個字符串中搜索給定字符來分解字符串ArrayList。根據字符的共同位置將列表分成新列表。如果數組是list1 = new String[] {"fish", "look", "flow", "fowl", "cool"}; 給定的字符是 'l' 那么我會得到 4 個新數組 no l "----"(fish), "l---"(look), "-l--"(flow), "-- -l"(雞,酷)。數組列表中將包含相應的字符串。我得到的錯誤是:java.lang.AssertionErrorArrayList<String> ret = f.familiesOf('l');        assertTrue(ret.contains("----"));    public Family_2(String[] w)    {        words = w;    }    /**     * Given a single character, return an ArrayList of     * all the word families. Each family should     * appear only once in the ArrayList and there should be none     * that aren't needed. The returned list can be in any order.     */    public ArrayList<String> familiesOf(char c)    {        String fam = "";        ArrayList<String> wordList = new ArrayList<String>();        ArrayList<String> wordList2 = new ArrayList<String>();        Collections.addAll(wordList, words);        String longestString = wordList.get(0);        // when I added the below code I stopped getting an out of bounds exception.        for (String element : wordList)        {            if (element.length() > longestString.length()) {                longestString = element;            }        }           // This is where I'm struggling with checking and separating the ArrayList.        for(int i = 0; i < words.length; i++)        {            if(words[i].indexOf(c) != c)            {                fam += '-';                 wordList2 = wordList;            }            else if(words[i].indexOf(c) == c)            {                fam += c;                wordList2 = wordList;            }        }        return wordList;    }這是劊子手游戲的前身。
查看完整描述

1 回答

?
呼喚遠方

TA貢獻1856條經驗 獲得超11個贊

我認為實現算法的關鍵是選擇正確的數據結構。我認為正確的數據結構是Map。鍵Map將是Integer(因為鍵不能是原語,所以它不能是int),它將是字母的索引,值將是在該索引處具有相關字母的單詞列表。

這是我根據您詳細說明的規范和限制實現該算法的代碼。

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;


public class HangsMan {


? ? public static void main(String[] args) {

? ? ? ? String[] words = new String[]{"fish", "look", "flow", "fowl", "cool", "eel", "poll", "fill"};

? ? ? ? char letter = 'l';

? ? ? ? Map<Integer, List<String>> theMap = new HashMap<>();

? ? ? ? Integer key;

? ? ? ? List<String> value;

? ? ? ? for (String word : words) {

? ? ? ? ? ? int ndx = word.indexOf(letter);

? ? ? ? ? ? int last = word.lastIndexOf(letter);

? ? ? ? ? ? if (last == ndx + 1) {

? ? ? ? ? ? ? ? ndx += 1_000_000;

? ? ? ? ? ? }

? ? ? ? ? ? key = Integer.valueOf(ndx);

? ? ? ? ? ? if (theMap.containsKey(key)) {

? ? ? ? ? ? ? ? value = theMap.get(key);

? ? ? ? ? ? }

? ? ? ? ? ? else {

? ? ? ? ? ? ? ? value = new ArrayList<String>();

? ? ? ? ? ? ? ? theMap.put(key, value);

? ? ? ? ? ? }

? ? ? ? ? ? value.add(word);

? ? ? ? }

? ? ? ? theMap.forEach((k, v) -> System.out.println(v));

? ? }

}

請注意,雙字母單詞會在索引中添加 1_000_000(一百萬),以便將它們與單字母單詞分開。因此, “poll”一詞的索引將為 1,000,002,而“ cold”一詞的索引僅為 2。


你問我為什么要加一百萬?因為,根據維基百科,英語中最長的單詞包含189,819 個字母。



查看完整回答
反對 回復 2023-10-19
  • 1 回答
  • 0 關注
  • 142 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號