【发布时间】:2020-07-25 23:34:52
【问题描述】:
使用 java.util.LinkedList 编写一个 java 程序,其中包含一个名为 getVowel() 的函数,该函数接受一个 String 类型的链表并仅返回包含元音字母作为首字母的数据值。
解释:如果linkkedList中的初始数据值为[Algorithms, Data, Computer, Information] getVowel()的返回值必须只有[Algorithms, Information],因为它们是唯一以元音开头的数据值算法中的 A 和信息中的 I 等字母。
getVowel() 函数不存在
import java.util.*;
public class g {
public static void main(String[] argv) throws Exception
{
try {
// creating object of LinkedList<String>
LinkedList<String> list = new LinkedList<String>();
// add some elements to list
list.add("Algorithms");
list.add("Information");
// print the linked list
System.out.println("LinkedList : " + list);
String value = list.getVowel();
// print the vowel of list
System.out.println("the vowel of list : " + value);
}
catch (NullPointerException e) {
System.out.println("Exception thrown : " + e);
}
}
}
【问题讨论】:
-
这是你实现方法
List<String> getWordsStartingWithVowel(List<String> inputList)的任务。您不能在 Java 集合中的 LinkedList 实例上调用 getVowel()。 -
附带问题:
catch (NullPointerException e) {...}-- 永远不要这样做
标签: java algorithm data-structures linked-list