Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package midtermq1;
- import java.util.ArrayList;
- /**
- *
- * @author steventroung
- */
- public class Q1 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- ArrayList<String> words = new ArrayList<String>();
- words.add("four");
- words.add("score");
- words.add("and");
- words.add("seven");
- words.add("years");
- words.add("ago");
- //here goes the line to display the ArrayList
- for (int i = 0; i < words.size(); i++) {
- System.out.print(words.get(i) + ", ");
- }
- //here is the call to the method you are to write
- System.out.println("\nThe smallest words is " + smallest(words));
- }
- public static String smallest(ArrayList<String> words) {
- String short = words.get(0);
- for (int i = 1; i < words.size(); i++) {
- if (words.get(i).length() < short.length()) {
- short = words.get(i);
- }
- }
- return short;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement