Question
import java.util.List; import java.util.ArrayList; /* * * This program loops through an array list of strings and finds the longest string */ public class FindLongest
import java.util.List;
import java.util.ArrayList;
/*
*
* This program loops through an array list of strings and finds the longest string
*/
public class FindLongest
{
/*
* Given the array list of strings, return the longest string
*/
public static String findLongest(ArrayList list)
{
//-----------Start below here. To do: approximate lines of code = 8
// loop through the list of strings and check to see if
// the string length is > the current longest string. If so, update the
// longest string length. Be sure to create a variable to hold the longest string itself
// as well as the length
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
public static void main(String[] args)
{
ArrayList elems = new ArrayList();
String[] words = {"singapore", "cattle", "metropolitan", "turnstile", "city", "deviation"};
for (int i = 0; i < words.length; i ++)
{
elems.add(words[i]);
}
System.out.println(findLongest(elems));
System.out.println("Expected:metropolitan");
elems.clear();
System.out.println(findLongest(elems));
System.out.println("Expected:");
}
}
P.S: Please write the code as well as include screenshots for reference along with output
Step by Step Solution
There are 3 Steps involved in it
Step: 1
import javautilList import javautilArrayList This program loops through an array list of strings and ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started