10个程序员常犯的错误

This list summarizes the top 10 mistakes that Java developers frequently make.

 

#1. Convert Array to ArrayList

To convert an array to an ArrayList, developers often do this:

List<</span>String> list = Arrays.asList(arr);

Arrays.asList() will return an ArrayList which is a private static class inside Arrays, it is not the java.util.ArrayList class. The java.util.Arrays.ArrayList class has set(),get()contains() methods, but does not have any methods for adding elements, so its size is fixed. To create a real ArrayList, you should do:

ArrayList<</span>String> arrayList = new ArrayList<</span>String>(Arrays.asList(arr));

The constructor of ArrayList can accept a Collection type, which is also a super type forjava.util.Arrays.ArrayList.

#2. Check If an Array Contains a Value

Developers often do:

Set<</span>String> set = new HashSet<</span>String>(Arrays.asList(arr));
return set.contains(targetValue);

The code works, but there is no need to convert a list to set first. Converting a list to a set requires extra time. It can as simple as:

Arrays.asList(arr).contains(targetValue);

or

for(String s: arr){
        if(s.equals(targetValue))
                return true;
}
return false;

The first one is more readable than the second one.

#3. Remove an Element from a List Inside a Loop

Consider the following code which removes elements during iteration:

ArrayList<</span>String> list = new ArrayList<</span>String>(Arrays.asList("a", "b", "c", "d"));
for (int i = 0; i <</span> list.size(); i++) {
        list.remove(i);
}
System.out.println(list);

The output is:

[b, d]

There is a serious problem in this method. When an element is removed, the size of the list shrinks and the index changes. So if you want to delete multiple elements inside a loop by using the index, that will not work properly.

You may know that using iterator is the right way to delete elements inside loops, and you know foreach loop in Java works like an iterator, but actually it is not. Consider the following code:

ArrayList<</span>String> list = new ArrayList<</span>String>(Arrays.asList("a", "b", "c", "d"));
 
for (String s : list) {
        if (s.equals("a"))
                list.remove(s);
}

It will throw out ConcurrentModificationException.

Instead the following is OK:

ArrayList<</span>String> list = new ArrayList<</span>String>(Arrays.asList("a", "b", "c", "d"));
Iterator<</span>String> iter = list.iterator();
while (iter.hasNext()) {
        String s = iter.next();
 
        if (s.equals("a")) {
                iter.remove();
        }
}

.next() must be called before .remove(). In the foreach loop, compiler will make the .next()called after the operation of removing element, which caused theConcurrentModificationException. You may want to take a look at the source code ofArrayList.iterator().

#4. Hashtable vs HashMap

By conventions in algorithm, Hashtable is the name of the data structure. But in Java, the data structure's name is HashMap. One of the key differences between Hashtable and HashMap is thatHashtable is synchronized. So very often you don't need Hashtable, instead HashMap should be used.

HashMap vs. TreeMap vs. Hashtable vs. LinkedHashMap
Top 10 questions about Map

#5. Use Raw Type of Collection

In Java, raw type and unbounded wildcard type are easy to mixed together. Take Set for example, Setis raw type, while Set

posted @ 2014-08-07 13:35  java_rookie  Views(200)  Comments(0)    收藏  举报