Synopsis: | Use asList instead of tight loops |
Language: | Java |
Severity Level: | 3 |
Category: | Optimization |
Description: |
The class java.util.Arrays has a "asList" method that
should be use when you want to create a new List from
an array of objects. It is faster than executing a loop to
cpy all the elements of the array one by onepublic class Test { public void foo(Integer[] ints) { // could just use Arrays.asList(ints) List l= new ArrayList(10); for (int i=0; i< 100; i++) { l.add(ints[i]); } for (int i=0; i< 100; i++) { l.add(a[i].toString()); // won't trigger the rule } } } |