使用 java.util.LinkedList 编写一个 Java 程序,其中包含一个名为 getVowel() 的函数,该函数接受 String 类型的链接列表,并仅返回包含元音字母作为第一个字母的数据值。
解释:如果 linkedList 中的初始数据值为 [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);
}
}
}
最佳答案
您需要编写一个如下所示的方法(或函数):
private LinkedList<String> getVowel(LinkedList<String> originalList)
{
// put your implementation/code here
}
在方法内部构建并返回一个新的链表,其中仅包含原始列表中所需的元素。
在您的 main
方法中调用您的 getVowel
方法并打印它返回的内容。
关于java - 编写一个 java 程序,其中包含一个名为 getVowel() 的函数,该函数采用链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61187389/