Web Hosting Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting

Alden Hosting provides professional, efficient, and reliable business-class Web hosting services to small- and medium-sized businesses.

WWW.

Call Us Toll-Free
(877) 256-0328

Outside USA
1 - (201) 505-0430

Web Hosting Welcome Web Hosting Web Hosting Plans Overview , Fund Raising, Fundraising, web hosting, website hosting, web site hosting Web Hosting Fund Raising, Fundraising, web hosting Web Hosting Resellers, web Hosting Web Hosting Web Design, web Hosting Web Hosting Extra Services,  web Hosting Web Hosting Traffic Booster, web hosting Web Hosting Traffic Booster, web hosting Web Hosting Technical Support,  web Hosting Web Hosting webmaster tips,  web Hosting Web Hosting 30 Day Money Back, web hosting Web Hosting Legal Notices for Web Hosting Web Hosting Glossary Computer Terms for web Hosting Web Hosting Contact Information - web hosting

Site Map

  Web Hosting Web Hosting Sign-Up   Web Hosting Fund Raising, Fundraising, web hosting, website hosting, web site hosting    Web Hosting Resellers web hosting, website hosting, web site hosting   Web Hosting EZ Site Control Panel for web hosting,website hosting, web site hosting
Answers to Questions and Exercises: (The Java™ Tutorials > Collections > Interfaces)
Trail: Collections
Lesson: Interfaces
Home Page > Collections > Interfaces
Answers to Questions and Exercises:

Questions

  1. Question: This lesson mentions three ways to traverse a List. Describe them, and note the limitations of each.

    Answer:

    • Use the enhanced for statement:
      List<Thing> list;
      ...
      for (Thing thing : list) {
          ...
      }
      
      Limitations: cannot be used to add, remove, or modify elements.
    • Use the traditional for statement together with an Iterator:
      List<Thing> list;
      ...
      for (Iterator<Thing> it = list.iterator(); it.hasNext(); ) {
          Thing thing = it.next();
          ...
      }
      
      Limitations: cannot be used to modify elements.
    • Use the traditional for statement together with an ListIterator:
      List<Thing> list;
      ...
      for (ListIterator<Thing> it = list.iterator(); it.hasNext(); ) {
          Thing thing = it.next();
          ...
      }
      
      Limitations: none.
  2. Question: Consider the four core interfaces, Set, List, Queue, and Map. For each of the following four assignments, specify which of the four core interfaces is best-suited, and explain how to use it to implement the assignment.

    Answer:

    • Whimsical Toys Inc (WTI) needs to record the names of all its employees. Every month, an employee will be chosen at random from these records to receive a free toy.
      Use a List. Choose a random employee by picking a number between 0 and size()-1.
    • WTI has decided that each new product will be named after an employee — but only first names will be used, and each name will be used only once. Prepare a list of unique first names.
      Use a Set. Collections that implement this interface don't allow the same element to be entered more than once.
    • WTI decides that it only wants to use the most popular names for its toys. Count up the number of employees who have each first name.
      Use a Map, where the keys are first names, and each value is a count of the number of employees with that first name.
    • WTI acquires season tickets for the local lacrosse team, to be shared by employees. Create a waiting list for this popular sport.
      Use a Queue. Invoke add() to add employees to the waiting list, and remove() to remove them.

  3. Question: The following program is supposed to print the string "Blue". Instead, it throws an error. Why?
    import java.util.*;
    
    public class SortMe {
        public static void main(String args[]) {
            SortedSet<StringBuffer> s = new TreeSet<StringBuffer>();
            s.add(new StringBuffer("Red"));
            s.add(new StringBuffer("White"));
            s.add(new StringBuffer("Blue"));
            System.out.println(s.first());
        }
    }
    

    Answer: TreeSort elements must be instances of a class that implements Comparable. StringBuffer does not.

Exercises

  1. Exercise: Write a program that prints its arguments in random order. Do not make a copy of the argument array.

    Answer:

    import java.util.*;
    
    public class Ran {
        public static void main(String[] args) {
            List<String> argList = Arrays.asList(args);
            Collections.shuffle(argList);
            for (String arg: argList) {
                System.out.format("%s ", arg);
            }
            System.out.println();
        }
    }
    
  2. Exercise: Take the FindDups example and modify it to use a SortedSet instead of a Set. Specify a Comparator so that case is ignored when sorting and identifying set elements.

    Answer:

    import java.util.*;
    
    public class FindDups {
    
        public static void main(String[] args) {
            Comparator<String> comparator = new Comparator<String>() {
                public int compare (String s1, String s2) {
                    return s1.compareToIgnoreCase(s2);
                }
            };
    
            SortedSet<String> s = new TreeSet<String>(comparator);
            for (String a : args)
                if (!s.add(a))
                    System.out.println("Duplicate detected: " + a);
    
            System.out.println(s.size() + " distinct words: " + s);
        }
    }
    
  3. Exercise: Write a method that takes a List<String> and applies String.trim to each element. To do this, you'll need to pick one of the three iteration idioms that you described in Question 1. Two of these will not give the result you want, so be sure to write a program that demonstrates that the method actually works!

    Answer: The enhanced for statement does not allow you to modify the List. Using an Iterator allows you to add and delete elements, but not replace existing element. That leaves ListIterator:

    import java.util.*;
    
    public class ListTrim {
        static void listTrim(List<String> strings) {
            for (ListIterator<String> lit = strings.listIterator();
                    lit.hasNext(); ) {
                lit.set(lit.next().trim());
            }
        }
    
        public static void main(String[] args) {
            List<String> l = Arrays.asList(" red ", " white ", " blue ");
            listTrim(l);
            for (String s : l) {
                System.out.format("\"%s\"%n", s);
            }
        }
    }
    
Previous page: Questions and Exercises: Interfaces
Web Hosting, web hosting, JSP, Servlets, Tomcat, website hosting, web site hosting
Add to My Yahoo!

XML icon

Add to Google

 

 

 

 

 

 

 

 

 

 

 

http://alden-servlet-Hosting.com
JSP at alden-servlet-Hosting.com
Servlets at alden-servlet-Hosting.com
Servlet at alden-servlet-Hosting.com
Tomcat at alden-servlet-Hosting.com
MySQL at alden-servlet-Hosting.com
Java at alden-servlet-Hosting.com
sFTP at alden-servlet-Hosting.com
http://alden-tomcat-Hosting.com
JSP at alden-tomcat-Hosting.com
Servlets at alden-tomcat-Hosting.com
Servlet at alden-tomcat-Hosting.com
Tomcat at alden-tomcat-Hosting.com
MySQL at alden-tomcat-Hosting.com
Java at alden-tomcat-Hosting.com
sFTP at alden-tomcat-Hosting.com
http://alden-sftp-Hosting.com
JSP at alden-sftp-Hosting.com
Servlets at alden-sftp-Hosting.com
Servlet at alden-sftp-Hosting.com
Tomcat at alden-sftp-Hosting.com
MySQL at alden-sftp-Hosting.com
Java at alden-sftp-Hosting.com
sFTP at alden-sftp-Hosting.com
http://alden-jsp-Hosting.com
JSP at alden-jsp-Hosting.com
Servlets at alden-jsp-Hosting.com
Servlet at alden-jsp-Hosting.com
Tomcat at alden-jsp-Hosting.com
MySQL at alden-jsp-Hosting.com
Java at alden-jsp-Hosting.com
sFTP at alden-jsp-Hosting.com
http://alden-java-Hosting.com
JSp at alden-java-Hosting.com
Servlets at alden-java-Hosting.com
Servlet at alden-java-Hosting.com
Tomcat at alden-java-Hosting.com
MySQL at alden-java-Hosting.com
Java at alden-java-Hosting.com
sFTP at alden-java-Hosting.com
JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP Servlets Tomcat mysql Java JSP at JSP.aldenWEBhosting.com Servlets at servlets.aldenWEBhosting.com Tomcat at Tomcat.aldenWEBhosting.com mysql at mysql.aldenWEBhosting.com Java at Java.aldenWEBhosting.com Web Hosts Portal Web Links Web Links Web Hosting JSP Solutions Web Links JSP Solutions Web Hosting Servlets Solutions Web Links Servlets Solutions Web Hosting Web Links Web Links . .
.
.
. .
. . . . jsp hosting servlets hosting web hosting web sites designed cheap web hosting web site hosting myspace web hosting