我想撰寫一個方法來查找最長的字串(單詞)。如果兩個單詞的長度相同,輸出應該是最長的單詞,輸出應該是:“不止一個最長的單詞”。
我使用了 ArrayList 并且幾乎有一個解決方案,但是出了點問題。情況是當兩個詞的長度相同時我遇到了問題。輸出為: 多于一個最長的詞 多于一個最長的詞 14 增量是最長的詞
請查看我的一段代碼并幫助我找到答案:)
public class LongestWord {
public static void main(String[] args) {
ArrayList<String> wordsList = new ArrayList<String>();
wordsList.add("december");
wordsList.add("california");
wordsList.add("cat");
wordsList.add("implementation");
wordsList.add("incrementation");
int largestString = wordsList.get(0).length();
int index = 0;
for (int i = 0; i < wordsList.size(); i ) {
if (wordsList.get(i).length() > largestString) {
largestString = wordsList.get(i).length();
index = i;
}else if(wordsList.get(i).length() == largestString){
largestString = wordsList.get(i).length();
index = i;
System.out.println("More than one longest word");
}
}
System.out.println(largestString " " wordsList.get(index) " is the longest word ");
}
}
uj5u.com熱心網友回復:
事實是,在遍歷整個串列之前,您無法說出最大的單詞是什么。
所以在串列上迭代
- 如果單詞大于以前的最大大小:清除串列并保存單詞
- 如果單詞的大小與最大大小相同:保存單詞
- 如果單詞更小:沒有
List<String> wordsList = Arrays.asList(
"december", "california", "cat",
"implementation", "incremntation");
int maxLength = Integer.MIN_VALUE;
List<String> largestStrings = new ArrayList<>();
for (String s : wordsList) {
if (s.length() > maxLength) {
maxLength = s.length();
largestStrings.clear();
largestStrings.add(s);
} else if (s.length() == maxLength) {
largestStrings.add(s);
}
}
if (largestStrings.size() > 1) {
System.out.println("More than one longest word");
System.out.println(largestStrings);
} else {
System.out.println(largestStrings.get(0) " is the longest word");
}
給
More than one longest word
[implementation, incrementation]
uj5u.com熱心網友回復:
阿茲羅是對的。您可以使用兩次迭代找出問題。我不確定,但下面的代碼有效
for (int i = 0; i < wordsList.size(); i ) {
if (wordsList.get(i).length() > largestString) {
largestString = wordsList.get(i).length();
index = i;
}
}
for (int i = 0; i < wordsList.size(); i ) {
if (wordsList.get(index).length() == wordsList.get(i).length()) {
System.out.println("More than one longest word");
break;
}
}
uj5u.com熱心網友回復:
您可以通過一次回圈迭代來做到這一點。隨時隨地存盤最長的單詞。
import java.util.*;
public class Test {
public static void main(String[] args) {
final Collection<String> words = Arrays.asList(
"december", "california", "cat",
"implementation", "incrementation");
final Collection<String> longestWords = findLongestWords(words);
if (longestWords.size() == 1) {
System.out.printf("The longest word is: %s\n", longestWords.iterator().next());
} else if (longestWords.size() > 1) {
System.out.printf("More than one longest word. The longest words are: %s\n", longestWords);
}
}
private static final Collection<String> findLongestWords(final Collection<String> words) {
// using a Set, so that duplicate words are stored only once.
final Set<String> longestWords = new HashSet<>();
// remember the current length of the longest word
int lengthOfLongestWord = Integer.MIN_VALUE;
// iterate over all the words
for (final String word : words) {
// the length of this word is longer than the previously though longest word. clear the list and update the longest length.
if (word.length() > lengthOfLongestWord) {
lengthOfLongestWord = word.length();
longestWords.clear();
}
// the length of this word is currently though to be the longest word, add it to the Set.
if (word.length() == lengthOfLongestWord) {
longestWords.add(word);
}
}
// return an unmodifiable Set containing the longest word(s)
return Collections.unmodifiableSet(longestWords);
}
}
uj5u.com熱心網友回復:
我的兩美分就可以在單個回圈中完成。可以進一步改進。
ArrayList<String> wordsList = new ArrayList<String>();
wordsList.add("december");
wordsList.add("california");
wordsList.add("cat");
wordsList.add("implementation");
wordsList.add("incrementation");
String result;
int length = Integer.MIN_VALUE;
Map<String,String> map = new HashMap<>();
for(String word: wordsList){
if(word.length() >= length) {
length = word.length();
if (map.containsKey(String.valueOf(word.length())) || map.containsKey( "X" word.length())) {
map.remove(String.valueOf(word.length()));
map.put("X" word.length(), word);
} else {
map.put(String.valueOf(word.length()), word);
}
}
}
result = map.get(String.valueOf(length)) == null ? "More than one longest word" :
map.get(String.valueOf(length)) " is the longest word";
System.out.println(result);
uj5u.com熱心網友回復:
我更改了您的代碼以建議對問題采取不同的方法。老實說,我希望你會發現它很有趣并且很有幫助。它有兩種不同的方式,一種不關心找到不止一個最長的單詞(它只標記第一個 - 但您可以根據自己的喜好更改它),另一種則這樣做。
第一個解決方案:
`
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class LongestWord {
public static void main(String[] args) {
List<String> wordsList = new ArrayList<>();
wordsList.add("december");
wordsList.add("california");
wordsList.add("cat");
wordsList.add("implementation");
wordsList.add("incrementation");
wordsList.stream()
.max(LongestWord::compare)
.ifPresent(a -> System.out.println(a.toUpperCase() " is the longest word with length of: " a.length()));
}
private static int compare(String a1, String a2) {
if (a1.length() > a2.length())
return 1;
else if (a1.length() == a2.length())
return 0;
return -1;
}
}
`
第二種解決方案:
`
public class LongestWord {
public static void main(String[] args) {
List<String> wordsList = new ArrayList<>();
wordsList.add("december");
wordsList.add("california");
wordsList.add("cat");
wordsList.add("implementation");
wordsList.add("incrementation");
int max_length = wordsList.stream()
.max(LongestWord::compare)
.map(String::length).orElse(0);
List<String> finalWordsList = wordsList.stream()
.filter(word -> word.length() == max_length)
.collect(Collectors.toList());
if (finalWordsList.size() > 1) {
System.out.println("More than one longest word");
} else {
System.out.println(finalWordsList.get(0) " is the longest word");
}
}
private static int compare(String a1, String a2) {
if (a1.length() > a2.length())
return 1;
else if (a1.length() == a2.length())
return 0;
return -1;
}
}
`
uj5u.com熱心網友回復:
這是一種方法。我正在使用一組來保存結果,因為如果存在重復的單詞,則沒有理由包含它們。
- 迭代單詞
- 如果當前的字長
> maxLength,清除設定并添加文字,和更新maxLength - 如果等于 maxLength,只需添加單詞。
List<String> wordsList = List.of("december", "implementation",
"california", "cat", "incrementation");
int maxLength = Integer.MIN_VALUE;
Set<String> results = new HashSet<>();
for (String word : wordsList) {
int len = word.length();
if (len >= maxLength) {
if (len > maxLength) {
results.clear();
maxLength = len;
}
results.add(word);
}
}
System.out.printf("The longest word%s -> %s%n", results.size() > 1 ? "s" : "", results);
印刷
The longest words -> [implementation, incrementation]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/400832.html
上一篇:從字典中提取元素并創建資料框
