2015-12-15 11:47:04 -08:00
|
|
|
// collections/AddingGroups.java
|
2016-12-30 17:23:13 -08:00
|
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Adding groups of elements to Collection objects
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class AddingGroups {
|
|
|
|
public static void main(String[] args) {
|
|
|
|
Collection<Integer> collection =
|
|
|
|
new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
|
|
|
|
Integer[] moreInts = { 6, 7, 8, 9, 10 };
|
|
|
|
collection.addAll(Arrays.asList(moreInts));
|
|
|
|
// Runs significantly faster, but you can't
|
|
|
|
// construct a Collection this way:
|
|
|
|
Collections.addAll(collection, 11, 12, 13, 14, 15);
|
|
|
|
Collections.addAll(collection, moreInts);
|
|
|
|
// Produces a list "backed by" an array:
|
2016-01-25 18:05:55 -08:00
|
|
|
List<Integer> list = Arrays.asList(16,17,18,19,20);
|
2015-06-15 17:47:35 -07:00
|
|
|
list.set(1, 99); // OK -- modify an element
|
2016-01-25 18:05:55 -08:00
|
|
|
// list.add(21); // Runtime error; the underlying
|
|
|
|
// array cannot be resized.
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|