| 7 | |
| 8 | public class AddingGroups { |
| 9 | public static void main(String[] args) { |
| 10 | Collection<Integer> collection = |
| 11 | new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); |
| 12 | Integer[] moreInts = { 6, 7, 8, 9, 10 }; |
| 13 | collection.addAll(Arrays.asList(moreInts)); |
| 14 | // Runs significantly faster, but you can't |
| 15 | // construct a Collection this way: |
| 16 | Collections.addAll(collection, 11, 12, 13, 14, 15); |
| 17 | Collections.addAll(collection, moreInts); |
| 18 | // Produces a list "backed by" an array: |
| 19 | List<Integer> list = Arrays.asList(16,17,18,19,20); |
| 20 | list.set(1, 99); // OK -- modify an element |
| 21 | // list.add(21); // Runtime error; the underlying |
| 22 | // array cannot be resized. |
| 23 | } |
| 24 | } |