我试图通过在每个字符串中搜索给定字符来分解字符串ArrayList。根据字符的共同位置将列表分成新列表。
如果数组是
list1 = new String[] {"fish", "look", "flow", "fowl", "cool"};
给定的字符是 'l' 那么我会得到 4 个新数组 no 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;
}
这是刽子手游戏的前身。
呼唤远方
相关分类