Question

In java Write and test a method that finds the longest string(s) in a list of...

In java

Write and test a method that finds the longest string(s) in a list of strings. If there is more than one string with the maximum length, ALL of them should be returned, in some appropriate data-structure.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AllLongest {

    public static List<String> getAllLongest(List<String> strings) {
        List<String> longest = new ArrayList<>();
        for (int i = 0; i < strings.size(); i++) {
            if (longest.isEmpty() || longest.get(0).length() < strings.get(i).length()) {
                longest.clear();
                longest.add(strings.get(i));
            } else if (longest.get(0).length() == strings.get(i).length()) {
                longest.add(strings.get(i));
            }
        }
        return longest;
    }

    public static void main(String[] args) {
        System.out.println(getAllLongest(Arrays.asList("hi", "hello", "there", "how", "are", "you?")));
    }
}
Add a comment
Know the answer?
Add Answer to:
In java Write and test a method that finds the longest string(s) in a list of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA!!!! Write a method that returns the longest common prefix between a phrase A and...

    IN JAVA!!!! Write a method that returns the longest common prefix between a phrase A and a phrase B. If the two phrases do not share a common prefix, return the empty string “no prefix”. Your method should either take two strings or two char arrays as arguments (A, B) and return a string. Test your method in the main with these three pairings. Example A: snowball B: snowcone Return: “snow” A: river B: rover Return: “r” A: monday B:...

  • In java please write a method List toList(String[] array) that converts its argument array of strings...

    In java please write a method List toList(String[] array) that converts its argument array of strings to a list of strings and then returns it.

  • Using the programming language Java: Write a function to find the longest common prefix string amongst...

    Using the programming language Java: Write a function to find the longest common prefix string amongst an array of strings.

  • in Java please (a) Write a static method abbreviate( ) which is passed a String s...

    in Java please (a) Write a static method abbreviate( ) which is passed a String s and an int max. The method returns a new String which contains the content of s abbreviated to at most max characters, but where the last three characters are ellipses (i.e., the characters . . .). For example, if s is "Too bad Spongebob’s not here to enjoy Spongebob not being here. ---Squidward." and max is 10, the method returns the 10-character String "Too...

  • Use Java to answer the question (Occurrences of a specified character) Write a method that finds...

    Use Java to answer the question (Occurrences of a specified character) Write a method that finds the number of occurrences of a special character in a string using the following header: public static int count (String str, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string followed by a character then displays the number of occurrences of the character in the string. In addition to the requirements described in...

  • 2a) Write a method countEvens that takes an ArrayList of String objects as input and returns...

    2a) Write a method countEvens that takes an ArrayList of String objects as input and returns the number of even length strings contained in the input. For example, if the input is [ one, peach, pear, plum ] then countEvents(inp) should return 2. 2b) Write a method, mirror, that doubles the size of a list of integers by appending a mirror image of the list. For example, given an array list containing [ 1, 5, 2, 6 ], the method...

  • Data Structure Hi, could you please help with the following question in Java a. Suppose we...

    Data Structure Hi, could you please help with the following question in Java a. Suppose we have some List of Strings called list and a String prefix. Write a method that removes all the Strings from list that begin with prefix. public static <E> void removePrefixStrings(List<String> list, String prefix) { write your method here } b. What is the time complexity of the method you wrote?

  • Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to...

    Java language Any use of java.util.LinkedList is prohibited Objective: The goal of this assignment is to practice recursion. ignment: The assignment requires writing recursive methods for some linked list operations. The use of loops in the recursive methods is strictly prohibited in this assignment. That is, you cannot use for, while, and do-while in the recursive methods you will write. You can only use a loop when you will initiate values for a linked list in the main method. You...

  • in java!! Write a method to check prefix of two strings that have more than 3...

    in java!! Write a method to check prefix of two strings that have more than 3 characters. A 3-char prefix of a word “class” will be “cla”. The method should take two strings as parameters, then compare 3-char prefix of two strings. Write a method to check suffix of two strings that have more than 3 characters. A 3-char suffix of a word “world” will be “rld”. The method should take two strings as parameters, then compare 3-char suffix of...

  • Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines...

    Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly braces { }, and brackets [].  For example, the string {a(b+ac)d} and  kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac)d} do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Write...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT