首页 > 解决方案 > 如何分解 ArrayList通过搜索给定字符并创建新的 ArrayList?

问题描述

我试图通过在每个字符串中搜索给定字符来分解字符串数组列表。根据字符的共同位置将列表分离成新的列表。

如果数组是

list1 = new String[] {"fish", "look", "flow", "fowl", "cool"}; 

并且给定的字符是'l'然后我会得到4个新数组没有 l "----"(fish), "l---"(look), "-l--"(flow), "-- -l"(禽,酷)。数组列表中将包含相应的字符串。我得到的错误是:

java.lang.AssertionError
ArrayList<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;
    }

这是创建刽子手游戏的前兆。

标签: javaarraysarraylist

解决方案


我认为实现算法的关键是选择正确的数据结构。我认为正确的数据结构是MapMap键是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个字母。


推荐阅读