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 * An indexable collection of objects with a length. | 8 * An indexable collection of objects with a length. |
9 * | 9 * |
10 * Subclasses of this class implement different kinds of lists. | 10 * Subclasses of this class implement different kinds of lists. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 * temporarily and is restored before continuing the iteration, the iterator | 44 * temporarily and is restored before continuing the iteration, the iterator |
45 * does not detect it. | 45 * does not detect it. |
46 * | 46 * |
47 * It is generally not allowed to modify the list's length (adding or removing | 47 * It is generally not allowed to modify the list's length (adding or removing |
48 * elements) while an operation on the list is being performed, | 48 * elements) while an operation on the list is being performed, |
49 * for example during a call to [forEach] or [sort]. | 49 * for example during a call to [forEach] or [sort]. |
50 * Changing the list's length while it is being iterated, either by iterating it | 50 * Changing the list's length while it is being iterated, either by iterating it |
51 * directly or through iterating an [Iterable] that is backed by the list, will | 51 * directly or through iterating an [Iterable] that is backed by the list, will |
52 * break the iteration. | 52 * break the iteration. |
53 */ | 53 */ |
54 abstract class List<E> implements Iterable<E> { | 54 abstract class List<E> implements Iterable<E>, EfficientLength { |
55 /** | 55 /** |
56 * Creates a list of the given length. | 56 * Creates a list of the given length. |
57 * | 57 * |
58 * The created list is fixed-length if [length] is provided. | 58 * The created list is fixed-length if [length] is provided. |
59 * | 59 * |
60 * List fixedLengthList = new List(3); | 60 * List fixedLengthList = new List(3); |
61 * fixedLengthList.length; // 3 | 61 * fixedLengthList.length; // 3 |
62 * fixedLengthList.length = 1; // Error | 62 * fixedLengthList.length = 1; // Error |
63 * | 63 * |
64 * The list has length 0 and is growable if [length] is omitted. | 64 * The list has length 0 and is growable if [length] is omitted. |
(...skipping 10 matching lines...) Expand all Loading... |
75 * The [length] must not be negative or null, if it is provided. | 75 * The [length] must not be negative or null, if it is provided. |
76 */ | 76 */ |
77 external factory List([int length]); | 77 external factory List([int length]); |
78 | 78 |
79 /** | 79 /** |
80 * Creates a fixed-length list of the given length, and initializes the | 80 * Creates a fixed-length list of the given length, and initializes the |
81 * value at each position with [fill]: | 81 * value at each position with [fill]: |
82 * | 82 * |
83 * new List<int>.filled(3, 0); // [0, 0, 0] | 83 * new List<int>.filled(3, 0); // [0, 0, 0] |
84 * | 84 * |
85 * The [length] must not be negative or null. | 85 * The [length] must be a non-negative integer. |
| 86 * |
| 87 * If the list is growable, changing its length will not initialize new |
| 88 * entries with [fill]. After being created and filled, the list is |
| 89 * no different from any other growable or fixed-length list |
| 90 * created using [List]. |
86 */ | 91 */ |
87 external factory List.filled(int length, E fill); | 92 external factory List.filled(int length, E fill, {bool growable: false}); |
88 | 93 |
89 /** | 94 /** |
90 * Creates a list containing all [elements]. | 95 * Creates a list containing all [elements]. |
91 * | 96 * |
92 * The [Iterator] of [elements] provides the order of the elements. | 97 * The [Iterator] of [elements] provides the order of the elements. |
93 * | 98 * |
94 * This constructor returns a growable list when [growable] is true; | 99 * This constructor returns a growable list when [growable] is true; |
95 * otherwise, it returns a fixed-length list. | 100 * otherwise, it returns a fixed-length list. |
96 */ | 101 */ |
97 external factory List.from(Iterable elements, { bool growable: true }); | 102 external factory List.from(Iterable elements, { bool growable: true }); |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 | 184 |
180 /** | 185 /** |
181 * Returns an [Iterable] of the objects in this list in reverse order. | 186 * Returns an [Iterable] of the objects in this list in reverse order. |
182 */ | 187 */ |
183 Iterable<E> get reversed; | 188 Iterable<E> get reversed; |
184 | 189 |
185 /** | 190 /** |
186 * Sorts this list according to the order specified by the [compare] function. | 191 * Sorts this list according to the order specified by the [compare] function. |
187 * | 192 * |
188 * The [compare] function must act as a [Comparator]. | 193 * The [compare] function must act as a [Comparator]. |
189 * List<String> numbers = ['one', 'two', 'three', 'four']; | 194 * |
| 195 * List<String> numbers = ['two', 'three', 'four']; |
190 * // Sort from shortest to longest. | 196 * // Sort from shortest to longest. |
191 * numbers.sort((x, y) => x.length.compareTo(y.length)); | 197 * numbers.sort((a, b) => a.length.compareTo(b.length)); |
192 * numbers.join(', '); // 'one, two, four, three' | 198 * print(numbers); // [two, four, three] |
193 * | 199 * |
194 * The default List implementations use [Comparable.compare] if | 200 * The default List implementations use [Comparable.compare] if |
195 * [compare] is omitted. | 201 * [compare] is omitted. |
196 * | 202 * |
197 * List<int> nums = [13, 2, -11]; | 203 * List<int> nums = [13, 2, -11]; |
198 * nums.sort(); | 204 * nums.sort(); |
199 nums.join(', '); // '-11, 2, 13' | 205 * print(nums); // [-11, 2, 13] |
| 206 * |
| 207 * A [Comparator] may compare objects as equal (return zero), even if they |
| 208 * are distinct objects. |
| 209 * The sort function is not guaranteed to be stable, so distinct objects |
| 210 * that compare as equal may occur in any order in the result: |
| 211 * |
| 212 * List<String> numbers = ['one', 'two', 'three', 'four']; |
| 213 * numbers.sort((a, b) => a.length.compareTo(b.length)); |
| 214 * print(numbers); // [one, two, four, three] OR [two, one, four, three] |
200 */ | 215 */ |
201 void sort([int compare(E a, E b)]); | 216 void sort([int compare(E a, E b)]); |
202 | 217 |
203 /** | 218 /** |
204 * Shuffles the elements of this list randomly. | 219 * Shuffles the elements of this list randomly. |
205 */ | 220 */ |
206 void shuffle([Random random]); | 221 void shuffle([Random random]); |
207 | 222 |
208 /** | 223 /** |
209 * Returns the first index of [element] in this list. | 224 * Returns the first index of [element] in this list. |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
465 * as values. The `Map.keys` [Iterable] iterates the indices of this list | 480 * as values. The `Map.keys` [Iterable] iterates the indices of this list |
466 * in numerical order. | 481 * in numerical order. |
467 * | 482 * |
468 * List<String> words = ['fee', 'fi', 'fo', 'fum']; | 483 * List<String> words = ['fee', 'fi', 'fo', 'fum']; |
469 * Map<int, String> map = words.asMap(); | 484 * Map<int, String> map = words.asMap(); |
470 * map[0] + map[1]; // 'feefi'; | 485 * map[0] + map[1]; // 'feefi'; |
471 * map.keys.toList(); // [0, 1, 2, 3] | 486 * map.keys.toList(); // [0, 1, 2, 3] |
472 */ | 487 */ |
473 Map<int, E> asMap(); | 488 Map<int, E> asMap(); |
474 } | 489 } |
OLD | NEW |