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. Does not support operations that changes length. | |
15 * * Growable list. Full implmentation of the interface. | |
floitsch
2013/02/26 13:54:19
implementation
Lasse Reichstein Nielsen
2013/02/26 15:26:00
Done.
| |
16 * * Unmodifiable list. A list where you can't change length or values. | |
floitsch
2013/02/26 13:54:19
... list. Changing the length or values yields an
Lasse Reichstein Nielsen
2013/02/26 15:26:00
Example is very long. And reversed doesn't return
| |
10 */ | 17 */ |
11 abstract class List<E> implements Collection<E> { | 18 abstract class List<E> implements Collection<E> { |
12 /** | 19 /** |
13 * Creates a list of the given [length]. | 20 * Creates a list of the given [length]. |
14 * | 21 * |
15 * The length of the returned list is not fixed. | 22 * The list is a fixed-length list if [length] is provided, and an empty |
23 * growable list if [length] is omitted. | |
16 */ | 24 */ |
17 external factory List([int length = 0]); | 25 external factory List([int length]); |
18 | 26 |
19 /** | 27 /** |
20 * Creates a fixed-sized list of the given [length] where each entry is | 28 * Creates a fixed-sized list of the given [length] where each entry is |
floitsch
2013/02/26 13:54:19
fixed-length
Lasse Reichstein Nielsen
2013/02/26 15:26:00
Done.
| |
21 * filled with [fill]. | 29 * filled with [fill]. |
22 */ | 30 */ |
23 external factory List.fixedLength(int length, {E fill: null}); | 31 external factory List.filled(int length, E fill); |
32 | |
33 /** | |
34 * *Deprecated*: Use `new List(count)` instead. | |
35 */ | |
36 factory List.fixedLength(int count) => new List(count); | |
24 | 37 |
25 /** | 38 /** |
26 * Creates an list with the elements of [other]. The order in | 39 * Creates an list with the elements of [other]. The order in |
27 * the list will be the order provided by the iterator of [other]. | 40 * the list will be the order provided by the iterator of [other]. |
28 * | 41 * |
29 * The length of the returned list is not fixed. | 42 * The length of the returned list is not fixed. |
30 */ | 43 */ |
31 factory List.from(Iterable other) { | 44 factory List.from(Iterable other, { bool growable: true }) { |
32 var list = new List<E>(); | 45 List<E> list = new List<E>(); |
33 for (E e in other) { | 46 for (E e in other) { |
34 list.add(e); | 47 list.add(e); |
35 } | 48 } |
36 return list; | 49 if (growable) return list; |
50 int length = list.length; | |
51 List<E> fixedList = new List<E>(length); | |
52 for (int i = 0; i < length; i++) { | |
53 fixedList[i] = list[i]; | |
54 } | |
55 return fixedList; | |
37 } | 56 } |
38 | 57 |
39 /** | 58 /** |
59 * Generate a `List` of elements. | |
60 * | |
61 * Generates a list of values, where the values are created by | |
62 * calling the [generator] function for each index in the range | |
63 * 0 .. [length] - 1. | |
64 * | |
65 * The created length's length is fixed unless [growable] is true. | |
66 */ | |
67 factory List.generate(int length, E generator(int index), | |
68 { bool growable: false }) { | |
69 List<E> result; | |
70 if (growable) { | |
71 result = <E>[]..length = length; | |
72 } else { | |
73 result = new List<E>(length); | |
74 } | |
75 for (int i = 0; i < length; i++) { | |
76 result[i] = generator(i); | |
77 } | |
78 return result; | |
79 } | |
80 | |
81 /** | |
40 * Returns the element at the given [index] in the list or throws | 82 * Returns the element at the given [index] in the list or throws |
41 * an [RangeError] if [index] is out of bounds. | 83 * an [RangeError] if [index] is out of bounds. |
42 */ | 84 */ |
43 E operator [](int index); | 85 E operator [](int index); |
44 | 86 |
45 /** | 87 /** |
46 * Sets the entry at the given [index] in the list to [value]. | 88 * Sets the entry at the given [index] in the list to [value]. |
47 * Throws an [RangeError] if [index] is out of bounds. | 89 * Throws an [RangeError] if [index] is out of bounds. |
48 */ | 90 */ |
49 void operator []=(int index, E value); | 91 void operator []=(int index, E value); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 * not extendable. | 222 * not extendable. |
181 * If [length] is 0, this method does not do anything. | 223 * If [length] is 0, this method does not do anything. |
182 * If [start] is the length of the list, this method inserts the | 224 * If [start] is the length of the list, this method inserts the |
183 * range at the end of the list. | 225 * range at the end of the list. |
184 * Throws an [ArgumentError] if [length] is negative. | 226 * Throws an [ArgumentError] if [length] is negative. |
185 * Throws an [RangeError] if [start] is negative or if | 227 * Throws an [RangeError] if [start] is negative or if |
186 * [start] is greater than the length of the list. | 228 * [start] is greater than the length of the list. |
187 */ | 229 */ |
188 void insertRange(int start, int length, [E fill]); | 230 void insertRange(int start, int length, [E fill]); |
189 } | 231 } |
OLD | NEW |