【问题标题】:write a java program that include a function called getVowel() that takes a linked list [closed]编写一个Java程序,其中包含一个名为getVowel()的函数,该函数采用链表[关闭]
【发布时间】: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&lt;String&gt; getWordsStartingWithVowel(List&lt;String&gt; inputList)的任务。您不能在 Java 集合中的 LinkedList 实例上调用 getVowel()。
  • 附带问题:catch (NullPointerException e) {...} -- 永远不要这样做

标签: java algorithm data-structures linked-list


【解决方案1】:

你需要这样写一个方法(或函数):

private LinkedList<String> getVowel(LinkedList<String> originalList)
{
    // put your implementation/code here
}

在方法内部构建并返回一个新的链表,其中仅包含原始列表中所需的元素。

在您的main 方法中调用您的getVowel 方法并打印它返回的内容。

【讨论】:

  • 对不起,我不太明白,你能把代码写在里面
  • V.V 这不是我的作业,我的朋友挑战我
  • 你的朋友会认为我解决挑战是个好主意吗?也许你可以说服你的朋友在这里发表评论?
  • 我不想输
  • 这一点我很清楚。不过,我来自哪里,欺骗朋友更糟糕。
猜你喜欢
  • 1970-01-01
  • 2021-03-31
  • 1970-01-01
  • 1970-01-01
  • 2020-06-28
  • 2011-06-10
  • 1970-01-01
  • 2020-01-13
  • 2020-02-04
相关资源
最近更新 更多