java - 编写一个 java 程序,其中包含一个名为 getVowel() 的函数,该函数采用链表

标签 java algorithm data-structures linked-list

使用 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/

相关文章:

java - RESTEasy 和客户端 IP 地址

string - CodeJam 2014 : Solution for The Repeater

algorithm - 在非图行中找到所有可能解决方案的递归算法

字符串排列秩+数据结构

java - 使用组合键映射 JPA 实体

JavaBridge errno=10054 被 Remotehost 关闭

algorithm - 判断两个词是否是彼此的字谜

c# - 如何使用循环链表解决 Josephus Elimination

python - 如何删除链表中的节点?

java - 使用 JSON 对象解析 JSON