Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 55 56 57 58 59 60 < 61 > 62 63 64 65 66 67 .. 166 >> Next

27 enum = v.elements();
28 while (enum.hasMoreElements())
29 {
30 String s = (String) enum.nextElement();
31 System.out.println(s);
32 }
33 }
34 }
Listing 21.1 (continued)
When run, Listing 21.1 produces the following:
E:\classes>java org.javapitfalls.item21.BadVisitor
one
four
What’s really there...
one
three
four
You would expect to have visited elements "one", "three", and "four", but instead only visited elements "one" and "four". The problem is that we are assuming that the Enumeration implementation and the Vector class work in sync, which is not the case. What has happened is the index integer (called count) is not modified when the Vector.remove() method is called. This is demonstrated in Figure 21.1. Listing 21.2 demonstrates how an iterator handles this situation.
Before remove()
Index (1)
"two"
"three"
"four"
After remove()
0
Index (2)
"three"
"four"
Figure 21.1 Under the hood of a Vector.
160 Item 21
01 02 package org.javapitfalls.item21;
03 04 import java.util.*;
05 public class BadVisitor2
06 {
07 public static void main(String args[])
08 {
09 Vector v = new Vector();
10 11 v.add("one"); v.add("two"); v.add("three") ; v.add("four");
12 Iterator iter = v.iterator();
13 while (iter.hasNext())
14 {
15 String s = (String) iter.next();
16 if (s.equals("two"))
17 v.remove("two");
18 else
19 {
20 // Visit
21 System.out.println(s);
22 }
23 24 }
25 // see what's left
26 System.out.println("What's really there... ");
27 iter = v.iterator();
28 while (iter.hasNext())
29 {
30 String s = (String) iter.next();
31 System.out.println(s);
32 }
33 }
34 }
Listing 21.2 BadVisitor2.java
When run, Listing 21.2 produces the following:
E:\classes>java org.javapitfalls.item21.BadVisitor2
one
Exception in thread "main" java.util.ConcurrentModificationException at
java.util.AbstractList$Itr.checkForComodification(AbstractList.java:445) at java.util.AbstractList$Itr.next(AbstractList.java:418) at
com.javaworld.jpitfalls.article5.BadVisitor2.main(BadVisitor2.java:15)
Use Iteration over Enumeration 161
As the output shows, the class implementing the Iterator interface specifically checks for this type of concurrent modification (modification outside of the iteration implemenation class while we are iterating) and throws an exception. It would be nice if the Enumeration implementation class was upgraded with this same behavior. Now let's examine the correct way to do this. Listing 21.3 demonstrates both visiting and modifying with an Iterator.
01 02 package org.javapitfalls.item21;
03 04 import java.util.*;
05 public class GoodVisitor
06 {
07 public static void main(String args[])
08 {
09 Vector v = new Vector();
10 11 v.add("one"); v.add("two"); v.add("three") ; v.add("four");
12 Iterator iter = v.iterator();
13 while (iter.hasNext())
14 {
15 String s = (String) iter.next();
16 if (s.equals("two"))
17 iter.remove();
18 else
19 {
20 // Visit
21 System.out.println(s);
22 }
23 24 }
25 // see what's left
26 System.out.println("What's really there... ");
27 iter = v.iterator();
28 while (iter.hasNext())
29 {
30 String s = (String) iter.next();
31 System.out.println(s);
32 }
33 }
34 }
Listing 21.3 GoodVisitor.java
When Listing 21.3 is run, it produces the following:
E:\classes>java org.javapitfalls.item21.GoodVisitor
one
162 Item 22
three
four
What's really there...
one
three
four
Notice that the remove method is performed via the Iterator class and not using the Vector class. So, in general, for classes that support it, use an Iterator over Enumeration. All classes that implement the Collection interface support iteration through the following method:
public Iterator iterator();
The classes that implement the Collection interface are AbstractCollection, AbstractList, AbstractSet, ArrayList, BeanContextServicesSupport, BeanContextSupport, HashSet, LinkedHashSet, LinkedList, TreeSet, and Vector. Some classes like Hashtable and HashMap (all the classes that implement the Map interface) indirectly support iteration by returning a Collection via the values() method. There are still some classes that only implement Enumeration like StringTokenizer, java.security.Permissions, and classes in Swing and JNDI.
Item 22: J2ME Performance and Pitfalls
The Java 2 Micro Edition (J2ME) is a subset of the Java platform created for developing applications on small footprint devices, like personal digital assistants (PDAs) and cell phones. These devices are significantly constrained in terms of processor speed, memory, and storage space. While the amounts vary between devices, you can easily expect several orders of magnitude difference between your desktop PC and these devices. There are three main classes of pitfalls associated with the J2ME platform: memory consumption pitfalls, performance pitfalls, and API differences. Both memory consumption and performance pitfalls have more to do with programming habit than the J2ME platform. The fact is our habits have developed around creating reusable, modular, and readable J2SE/J2EE programs where memory and processing speed are abundant. Unfortunately, those very habits deemed “good style" for J2SE/J2EE lead to either nonperforming or poor-performing J2ME applications. The API pitfalls relate to different method calls for classes implemented in both J2SE and J2ME.
Previous << 1 .. 55 56 57 58 59 60 < 61 > 62 63 64 65 66 67 .. 166 >> Next