| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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>, EfficientLength { | 54 abstract class List<E> implements Iterable<E>, EfficientLength { |
| 55 @patch | 55 /** |
| 56 * Creates a list of the given length. |
| 57 * |
| 58 * The created list is fixed-length if [length] is provided. |
| 59 * |
| 60 * List fixedLengthList = new List(3); |
| 61 * fixedLengthList.length; // 3 |
| 62 * fixedLengthList.length = 1; // Error |
| 63 * |
| 64 * The list has length 0 and is growable if [length] is omitted. |
| 65 * |
| 66 * List growableList = new List(); |
| 67 * growableList.length; // 0; |
| 68 * growableList.length = 3; |
| 69 * |
| 70 * To create a growable list with a given length, just assign the length |
| 71 * right after creation: |
| 72 * |
| 73 * List growableList = new List()..length = 500; |
| 74 * |
| 75 * The [length] must not be negative or null, if it is provided. |
| 76 */ |
| 56 factory List([int length = const _ListConstructorSentinel()]) { | 77 factory List([int length = const _ListConstructorSentinel()]) { |
| 57 if (length == const _ListConstructorSentinel()) { | 78 if (length == const _ListConstructorSentinel()) { |
| 58 return new JSArray<E>.emptyGrowable(); | 79 return new JSArray<E>.emptyGrowable(); |
| 59 } | 80 } |
| 60 return new JSArray<E>.fixed(length); | 81 return new JSArray<E>.fixed(length); |
| 61 } | 82 } |
| 62 | 83 |
| 63 @patch | 84 /** |
| 85 * Creates a fixed-length list of the given length, and initializes the |
| 86 * value at each position with [fill]: |
| 87 * |
| 88 * new List<int>.filled(3, 0); // [0, 0, 0] |
| 89 * |
| 90 * The [length] must not be negative or null. |
| 91 */ |
| 64 factory List.filled(int length, E fill) { | 92 factory List.filled(int length, E fill) { |
| 65 List result = new JSArray<E>.fixed(length); | 93 List result = new JSArray<E>.fixed(length); |
| 66 if (length != 0 && fill != null) { | 94 if (length != 0 && fill != null) { |
| 67 for (int i = 0; i < result.length; i++) { | 95 for (int i = 0; i < result.length; i++) { |
| 68 result[i] = fill; | 96 result[i] = fill; |
| 69 } | 97 } |
| 70 } | 98 } |
| 71 return result; | 99 return result; |
| 72 } | 100 } |
| 73 | 101 |
| 74 @patch | 102 /** |
| 103 * Creates a list containing all [elements]. |
| 104 * |
| 105 * The [Iterator] of [elements] provides the order of the elements. |
| 106 * |
| 107 * This constructor returns a growable list when [growable] is true; |
| 108 * otherwise, it returns a fixed-length list. |
| 109 */ |
| 75 factory List.from(Iterable elements, { bool growable: true }) { | 110 factory List.from(Iterable elements, { bool growable: true }) { |
| 76 List<E> list = new List<E>(); | 111 List<E> list = new List<E>(); |
| 77 for (E e in elements) { | 112 for (E e in elements) { |
| 78 list.add(e); | 113 list.add(e); |
| 79 } | 114 } |
| 80 if (growable) return list; | 115 if (growable) return list; |
| 81 return makeListFixedLength(list); | 116 return makeListFixedLength(list); |
| 82 } | 117 } |
| 83 | 118 |
| 84 /** | 119 /** |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 * as values. The `Map.keys` [Iterable] iterates the indices of this list | 471 * as values. The `Map.keys` [Iterable] iterates the indices of this list |
| 437 * in numerical order. | 472 * in numerical order. |
| 438 * | 473 * |
| 439 * List<String> words = ['fee', 'fi', 'fo', 'fum']; | 474 * List<String> words = ['fee', 'fi', 'fo', 'fum']; |
| 440 * Map<int, String> map = words.asMap(); | 475 * Map<int, String> map = words.asMap(); |
| 441 * map[0] + map[1]; // 'feefi'; | 476 * map[0] + map[1]; // 'feefi'; |
| 442 * map.keys.toList(); // [0, 1, 2, 3] | 477 * map.keys.toList(); // [0, 1, 2, 3] |
| 443 */ | 478 */ |
| 444 Map<int, E> asMap(); | 479 Map<int, E> asMap(); |
| 445 } | 480 } |
| OLD | NEW |