3 ms·
For initializing lists there is a less verbose expression in Java: List<Integer> list = Arrays.asList(1,2,3,4,5);
by fi0660 17y ago
For initializing lists there is a less verbose expression in Java:
List<Integer> list = Arrays.asList(1,2,3,4,5);
- akeefer 17y ago. . . except that doesn't get you a modifiable list, it just wraps a List interface around the array that's implicitly created by the var-arg, so it's a trick you have to be careful with. Personally I often end up writing little helpers like: List<T> list(T... args) { return new ArrayList<T>(Arrays.asList(args)) } usually in test code. A built-in List and Map-initialization syntax is really something every decent programming language ought to have.
- litewulf 17y ago(Consider using the Google collections library if possible. It already has that in the form of classes like Lists, Maps, etc that provide builders and factory methods.)