OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * A [List] is an indexable collection with a length. It can be of | 8 * A [List] is an indexable collection with a length. |
9 * fixed size or extendable. | 9 * |
10 * A `List` implementation can be choose not to support all methods | |
11 * of the `List` interface. | |
12 * | |
13 * The most common list types are: | |
14 * * Fixed length list. It is an error to use operations that can change | |
15 * the list's length. | |
16 * * Growable list. Full implementation of the interface. | |
17 * * Unmodifiable list. It is an error to use operations that can change | |
18 * the list's length, or that can change the values of the list. | |
19 * If an unmodifable list is backed by another modifiable data structure, | |
20 * the values read from it may still change over time. | |
21 * | |
22 * Example: | |
23 * | |
24 * var fixedLengthList = new List(5); | |
25 * fixedLengthList.length = 0; // throws. | |
26 * fixedLengthList.add(499); // throws | |
27 * fixedLengthList[0] = 87; | |
28 * var growableList = [1, 2]; | |
29 * growableList.length = 0; | |
30 * growableList.add(499); | |
31 * growableList[0] = 87; | |
32 * var unmodifiableList = const [1, 2]; | |
33 * unmodifiableList.length = 0; // throws. | |
34 * unmodifiableList.add(499); // throws | |
35 * unmodifiableList[0] = 87; // throws. | |
10 */ | 36 */ |
11 abstract class List<E> implements Collection<E> { | 37 abstract class List<E> implements Collection<E> { |
12 /** | 38 /** |
13 * Creates a list of the given [length]. | 39 * Creates a list of the given [length]. |
14 * | 40 * |
15 * The length of the returned list is not fixed. | 41 * The list is a fixed-length list if [length] is provided, and an empty |
42 * growable list if [length] is omitted. | |
ngeoffray
2013/02/27 08:59:12
Shouldn't you say is omitted or null?
floitsch
2013/02/27 10:04:58
No. null is an error.
ngeoffray
2013/02/27 12:02:32
Maybe add that too in a comment?
| |
16 */ | 43 */ |
17 external factory List([int length = 0]); | 44 external factory List([int length]); |
18 | 45 |
19 /** | 46 /** |
20 * Creates a fixed-sized list of the given [length] where each entry is | 47 * Creates a fixed-length list of the given [length] where each entry |
21 * filled with [fill]. | 48 * contains [fill]. |
22 */ | 49 */ |
23 external factory List.fixedLength(int length, {E fill: null}); | 50 external factory List.filled(int length, E fill); |
51 | |
52 /** | |
53 * *Deprecated*: Use `new List(count)` instead. | |
54 */ | |
55 factory List.fixedLength(int count) => new List(count); | |
24 | 56 |
25 /** | 57 /** |
26 * Creates an list with the elements of [other]. The order in | 58 * Creates an list with the elements of [other]. The order in |
27 * the list will be the order provided by the iterator of [other]. | 59 * the list will be the order provided by the iterator of [other]. |
28 * | 60 * |
29 * The length of the returned list is not fixed. | 61 * The length of the returned list is not fixed. |
30 */ | 62 */ |
31 factory List.from(Iterable other) { | 63 factory List.from(Iterable other, { bool growable: true }) { |
32 var list = new List<E>(); | 64 List<E> list = new List<E>(); |
33 for (E e in other) { | 65 for (E e in other) { |
34 list.add(e); | 66 list.add(e); |
35 } | 67 } |
36 return list; | 68 if (growable) return list; |
69 int length = list.length; | |
70 List<E> fixedList = new List<E>(length); | |
71 for (int i = 0; i < length; i++) { | |
72 fixedList[i] = list[i]; | |
73 } | |
74 return fixedList; | |
37 } | 75 } |
38 | 76 |
39 /** | 77 /** |
78 * Generate a `List` of elements. | |
79 * | |
80 * Generates a list of values, where the values are created by | |
81 * calling the [generator] function for each index in the range | |
82 * 0 .. [length] - 1. | |
83 * | |
84 * The created length's length is fixed unless [growable] is true. | |
85 */ | |
86 factory List.generate(int length, E generator(int index), | |
87 { bool growable: false }) { | |
88 List<E> result; | |
89 if (growable) { | |
90 result = <E>[]..length = length; | |
91 } else { | |
92 result = new List<E>(length); | |
93 } | |
94 for (int i = 0; i < length; i++) { | |
95 result[i] = generator(i); | |
96 } | |
97 return result; | |
98 } | |
99 | |
100 /** | |
40 * Returns the element at the given [index] in the list or throws | 101 * Returns the element at the given [index] in the list or throws |
41 * an [RangeError] if [index] is out of bounds. | 102 * an [RangeError] if [index] is out of bounds. |
42 */ | 103 */ |
43 E operator [](int index); | 104 E operator [](int index); |
44 | 105 |
45 /** | 106 /** |
46 * Sets the entry at the given [index] in the list to [value]. | 107 * Sets the entry at the given [index] in the list to [value]. |
47 * Throws an [RangeError] if [index] is out of bounds. | 108 * Throws an [RangeError] if [index] is out of bounds. |
48 */ | 109 */ |
49 void operator []=(int index, E value); | 110 void operator []=(int index, E value); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 * not extendable. | 241 * not extendable. |
181 * If [length] is 0, this method does not do anything. | 242 * If [length] is 0, this method does not do anything. |
182 * If [start] is the length of the list, this method inserts the | 243 * If [start] is the length of the list, this method inserts the |
183 * range at the end of the list. | 244 * range at the end of the list. |
184 * Throws an [ArgumentError] if [length] is negative. | 245 * Throws an [ArgumentError] if [length] is negative. |
185 * Throws an [RangeError] if [start] is negative or if | 246 * Throws an [RangeError] if [start] is negative or if |
186 * [start] is greater than the length of the list. | 247 * [start] is greater than the length of the list. |
187 */ | 248 */ |
188 void insertRange(int start, int length, [E fill]); | 249 void insertRange(int start, int length, [E fill]); |
189 } | 250 } |
OLD | NEW |