Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1128)

Side by Side Diff: sdk/lib/core/list.dart

Issue 23908003: Added examples to list.dart. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Post LGTM tweaks. Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.
11 * The most common kinds of lists are: 11 * The most common kinds of lists are:
12 * 12 *
13 * * Fixed-length list. 13 * * Fixed-length list.
14 * An error occurs when attempting to use operations 14 * An error occurs when attempting to use operations
15 * that can change the length of the list. 15 * that can change the length of the list.
16 * 16 *
17 * * Growable list. Full implementation of the API defined in this class. 17 * * Growable list. Full implementation of the API defined in this class.
18 * 18 *
19 * The following code illustrates that some List implementations support 19 * The following code illustrates that some List implementations support
20 * only a subset of the API. 20 * only a subset of the API.
21 * 21 *
22 * var fixedLengthList = new List(5); 22 * List<int> fixedLengthList = new List(5);
23 * fixedLengthList.length = 0; // Error. 23 * fixedLengthList.length = 0; // Error
24 * fixedLengthList.add(499); // Error. 24 * fixedLengthList.add(499); // Error
25 * fixedLengthList[0] = 87; 25 * fixedLengthList[0] = 87;
26 * 26 * List<int> growableList = [1, 2];
27 * var growableList = [1, 2];
28 * growableList.length = 0; 27 * growableList.length = 0;
29 * growableList.add(499); 28 * growableList.add(499);
30 * growableList[0] = 87; 29 * growableList[0] = 87;
31 * 30 *
32 * Lists are [Iterable]. 31 * Lists are [Iterable]. Iteration occurs over values in index order. Changing
33 * Iteration occurs over values in index order. 32 * the values does not affect iteration, but changing the valid
34 * Changing the values does not affect iteration, 33 * indices&mdash;that is, changing the list's length&mdash;between iteration
35 * but changing the valid indices&mdash;that is, 34 * steps causes a [ConcurrentModificationError]. This means that only growable
36 * changing the list's length&mdash;between 35 * lists can throw ConcurrentModificationError. If the length changes
37 * iteration steps 36 * temporarily and is restored before continuing the iteration, the iterator
38 * causes a [ConcurrentModificationError]. 37 * does not detect it.
39 * This means that only growable lists can throw ConcurrentModificationError.
40 * If the length changes temporarily
41 * and is restored before continuing the iteration,
42 * the iterator does not detect it.
43 */ 38 */
44 abstract class List<E> implements Iterable<E> { 39 abstract class List<E> implements Iterable<E> {
45 /** 40 /**
46 * Creates a list of the given _length_. 41 * Creates a list of the given length.
47 * 42 *
48 * The created list is fixed-length if _length_ is provided. 43 * The created list is fixed-length if [length] is provided.
49 * The list has length 0 and is growable if _length_ is omitted.
50 * 44 *
51 * An error occurs if _length_ is negative. 45 * List fixedLengthList = new List(3);
46 * fixedLengthList.length; // 3
47 fixedLengthList.length = 1; // Error
48 *
49 *
50 * The list has length 0 and is growable if [length] is omitted.
51 *
52 * List growableList = new List();
53 * growableList.length; // 0;
54 * growableList.length = 3;
55 *
56 * An error occurs if [length] is negative.
52 */ 57 */
53 external factory List([int length]); 58 external factory List([int length]);
54 59
55 /** 60 /**
56 * Creates a fixed-length list of the given _length_ 61 * Creates a fixed-length list of the given length, and initializes the
57 * and initializes the value at each position with [fill]. 62 * value at each position with [fill]:
63 *
64 * new List<int>.filled(3, 0); // [0, 0, 0]
58 */ 65 */
59 external factory List.filled(int length, E fill); 66 external factory List.filled(int length, E fill);
60 67
61 /** 68 /**
62 * Creates a list and initializes it using the contents of [other]. 69 * Creates a list and initializes it using the contents of [other].
63 * 70 *
64 * The [Iterator] of [other] provides the order of the objects. 71 * The [Iterator] of [other] provides the order of the objects.
65 * 72 *
66 * This constructor returns a growable list if [growable] is true; 73 * This constructor returns a growable list if [growable] is true;
67 * otherwise, it returns a fixed-length list. 74 * otherwise, it returns a fixed-length list.
68 */ 75 */
69 factory List.from(Iterable other, { bool growable: true }) { 76 factory List.from(Iterable other, { bool growable: true }) {
70 List<E> list = new List<E>(); 77 List<E> list = new List<E>();
71 for (E e in other) { 78 for (E e in other) {
72 list.add(e); 79 list.add(e);
73 } 80 }
74 if (growable) return list; 81 if (growable) return list;
75 int length = list.length; 82 int length = list.length;
76 List<E> fixedList = new List<E>(length); 83 List<E> fixedList = new List<E>(length);
77 for (int i = 0; i < length; i++) { 84 for (int i = 0; i < length; i++) {
78 fixedList[i] = list[i]; 85 fixedList[i] = list[i];
79 } 86 }
80 return fixedList; 87 return fixedList;
81 } 88 }
82 89
83 /** 90 /**
84 * Generates a list of values. 91 * Generates a list of values.
85 * 92 *
86 * Creates a list with _length_ positions 93 * Creates a list with [length] positions and fills it with values created by
87 * and fills it with values created by calling [generator] 94 * calling [generator] for each index in the range `0` .. `length - 1`
88 * for each index in the range `0` .. `length - 1`
89 * in increasing order. 95 * in increasing order.
90 * 96 *
97 * new List<int>.generate(3, (int index) => index * index); // [0, 1, 4]
98 *
91 * The created list is fixed-length unless [growable] is true. 99 * The created list is fixed-length unless [growable] is true.
92 */ 100 */
93 factory List.generate(int length, E generator(int index), 101 factory List.generate(int length, E generator(int index),
94 { bool growable: true }) { 102 { bool growable: true }) {
95 List<E> result; 103 List<E> result;
96 if (growable) { 104 if (growable) {
97 result = <E>[]..length = length; 105 result = <E>[]..length = length;
98 } else { 106 } else {
99 result = new List<E>(length); 107 result = new List<E>(length);
100 } 108 }
(...skipping 19 matching lines...) Expand all
120 * Returns the number of objects in this list. 128 * Returns the number of objects in this list.
121 * 129 *
122 * The valid indices for a list are `0` through `length - 1`. 130 * The valid indices for a list are `0` through `length - 1`.
123 */ 131 */
124 int get length; 132 int get length;
125 133
126 /** 134 /**
127 * Changes the length of this list. 135 * Changes the length of this list.
128 * 136 *
129 * If [newLength] is greater than 137 * If [newLength] is greater than
130 * the current [length], entries are initialized to [:null:]. 138 * the current length, entries are initialized to [:null:].
131 * 139 *
132 * Throws an [UnsupportedError] if the list is fixed-length. 140 * Throws an [UnsupportedError] if the list is fixed-length.
133 */ 141 */
134 void set length(int newLength); 142 void set length(int newLength);
135 143
136 /** 144 /**
137 * Adds [value] to the end of this list, 145 * Adds [value] to the end of this list,
138 * extending the length by one. 146 * extending the length by one.
139 * 147 *
140 * Throws an [UnsupportedError] if the list is fixed-length. 148 * Throws an [UnsupportedError] if the list is fixed-length.
(...skipping 10 matching lines...) Expand all
151 159
152 /** 160 /**
153 * Returns an [Iterable] of the objects in this list in reverse order. 161 * Returns an [Iterable] of the objects in this list in reverse order.
154 */ 162 */
155 Iterable<E> get reversed; 163 Iterable<E> get reversed;
156 164
157 /** 165 /**
158 * Sorts this list according to the order specified by the [compare] function. 166 * Sorts this list according to the order specified by the [compare] function.
159 * 167 *
160 * The [compare] function must act as a [Comparator]. 168 * The [compare] function must act as a [Comparator].
169
170 * List<String> numbers = ['one', 'two', 'three', 'four'];
171 * // Sort from shortest to longest.
172 * numbers.sort((x, y) => x.length.compareTo(y.length));
173 * numbers.join(', '); // 'one, two, four, three'
161 * 174 *
162 * The default List implementations use [Comparable.compare] if 175 * The default List implementations use [Comparable.compare] if
163 * [compare] is omitted. 176 * [compare] is omitted.
177 *
178 * List<int> nums = [13, 2, -11];
179 * nums.sort();
180 nums.join(', '); // '-11, 2, 13'
164 */ 181 */
165 void sort([int compare(E a, E b)]); 182 void sort([int compare(E a, E b)]);
166 183
167 /** 184 /**
168 * Returns the first index of [element] in this list. 185 * Returns the first index of [element] in this list.
169 * 186 *
170 * Searches the list from index [start] to the length of the list. 187 * Searches the list from index [start] to the end of the list.
171 * The first time an object [:o:] is encountered so that [:o == element:], 188 * The first time an object [:o:] is encountered so that [:o == element:],
172 * the index of [:o:] is returned. 189 * the index of [:o:] is returned.
190 *
191 * List<String> notes = ['do', 're', 'mi', 're'];
192 * notes.indexOf('re'); // 1
193 * notes.indexOf('re', 2); // 3
194 *
173 * Returns -1 if [element] is not found. 195 * Returns -1 if [element] is not found.
196 *
197 * notes.indexOf('fa'); // -1
174 */ 198 */
175 int indexOf(E element, [int start = 0]); 199 int indexOf(E element, [int start = 0]);
176 200
177 /** 201 /**
178 * Returns the last index of [element] in this list. 202 * Returns the last index of [element] in this list.
179 * 203 *
180 * Searches the list backwards from index [start] to 0. 204 * Searches the list backwards from index [start] to 0.
181 * 205 *
182 * The first time an object [:o:] is encountered so that [:o == element:], 206 * The first time an object [:o:] is encountered so that [:o == element:],
183 * the index of [:o:] is returned. 207 * the index of [:o:] is returned.
184 * 208 *
185 * If [start] is not provided, it defaults to [:this.length - 1:]. 209 * List<String> notes = ['do', 're', 'mi', 're'];
210 * notes.lastIndexOf('re', 2); // 1
211 *
212 * If [start] is not provided, this method searches from the end of the
213 * list./Returns
214 *
215 * notes.lastIndexOf('re'); // 3
186 * 216 *
187 * Returns -1 if [element] is not found. 217 * Returns -1 if [element] is not found.
218 *
219 * notes.lastIndexOf('fa'); // -1
188 */ 220 */
189 int lastIndexOf(E element, [int start]); 221 int lastIndexOf(E element, [int start]);
190 222
191 /** 223 /**
192 * Removes all objects from this list; 224 * Removes all objects from this list;
193 * the length of the list becomes zero. 225 * the length of the list becomes zero.
194 * 226 *
195 * Throws an [UnsupportedError], and retains all objects, if this 227 * Throws an [UnsupportedError], and retains all objects, if this
196 * is a fixed-length list. 228 * is a fixed-length list.
197 */ 229 */
(...skipping 18 matching lines...) Expand all
216 * 248 *
217 * An error occurs if the [index] is less than 0 or greater than length. 249 * An error occurs if the [index] is less than 0 or greater than length.
218 * An [UnsupportedError] occurs if the list is fixed-length. 250 * An [UnsupportedError] occurs if the list is fixed-length.
219 */ 251 */
220 void insertAll(int index, Iterable<E> iterable); 252 void insertAll(int index, Iterable<E> iterable);
221 253
222 /** 254 /**
223 * Overwrites objects of `this` with the objects of [iterable], starting 255 * Overwrites objects of `this` with the objects of [iterable], starting
224 * at position [index] in this list. 256 * at position [index] in this list.
225 * 257 *
258 * List<String> list = ['a', 'b', 'c'];
259 * list.setAll(1, ['bee', 'sea']);
260 * list.join(', '); // 'a, bee, sea'
261 *
226 * This operation does not increase the length of `this`. 262 * This operation does not increase the length of `this`.
227 * 263 *
228 * An error occurs if the [index] is less than 0 or greater than length. 264 * An error occurs if the [index] is less than 0 or greater than length.
229 * An error occurs if the [iterable] is longer than [length] - [index]. 265 * An error occurs if the [iterable] is longer than [length] - [index].
230 */ 266 */
231 void setAll(int index, Iterable<E> iterable); 267 void setAll(int index, Iterable<E> iterable);
232 268
233 /** 269 /**
234 * Removes the first occurence of [value] from this list. 270 * Removes the first occurence of [value] from this list.
235 * 271 *
236 * Returns true if [value] was in the list. 272 * Returns true if [value] was in the list, false otherwise.
237 * Returns false otherwise. 273 *
274 * List<String> parts = ['head', 'shoulders', 'knees', 'toes'];
275 * parts.remove('head'); // true
276 * parts.join(', '); // 'shoulders, knees, toes'
238 * 277 *
239 * The method has no effect if [value] was not in the list. 278 * The method has no effect if [value] was not in the list.
240 * 279 *
280 * // Note: 'head' has already been removed.
281 * parts.remove('head'); // false
282 * parts.join(', '); // 'shoulders, knees, toes'
283 *
241 * An [UnsupportedError] occurs if the list is fixed-length. 284 * An [UnsupportedError] occurs if the list is fixed-length.
242 */ 285 */
243 bool remove(Object value); 286 bool remove(Object value);
244 287
245 /** 288 /**
246 * Removes the object at position [index] from this list. 289 * Removes the object at position [index] from this list.
247 * 290 *
248 * This method reduces the length of `this` by one and moves all later objects 291 * This method reduces the length of `this` by one and moves all later objects
249 * down by one position. 292 * down by one position.
250 * 293 *
(...skipping 11 matching lines...) Expand all
262 * 305 *
263 * Throws an [UnsupportedError] if this is a fixed-length list. 306 * Throws an [UnsupportedError] if this is a fixed-length list.
264 */ 307 */
265 E removeLast(); 308 E removeLast();
266 309
267 /** 310 /**
268 * Removes all objects from this list that satisfy [test]. 311 * Removes all objects from this list that satisfy [test].
269 * 312 *
270 * An object [:o:] satisfies [test] if [:test(o):] is true. 313 * An object [:o:] satisfies [test] if [:test(o):] is true.
271 * 314 *
315 * List<String> numbers = ['one', 'two', 'three', 'four'];
316 * numbers.removeWhere((item) => item.length == 3);
317 * numbers.join(', '); // 'three, four'
318 *
272 * Throws an [UnsupportedError] if this is a fixed-length list. 319 * Throws an [UnsupportedError] if this is a fixed-length list.
273 */ 320 */
274 void removeWhere(bool test(E element)); 321 void removeWhere(bool test(E element));
275 322
276 /** 323 /**
277 * Removes all objects from this list that fail to satisfy [test]. 324 * Removes all objects from this list that fail to satisfy [test].
278 * 325 *
279 * An object [:o:] satisfies [test] if [:test(o):] is true. 326 * An object [:o:] satisfies [test] if [:test(o):] is true.
280 * 327 *
328 * List<String> numbers = ['one', 'two', 'three', 'four'];
329 * numbers.retainWhere((item) => item.length == 3);
330 * numbers.join(', '); // 'one, two'
331 *
281 * Throws an [UnsupportedError] if this is a fixed-length list. 332 * Throws an [UnsupportedError] if this is a fixed-length list.
282 */ 333 */
283 void retainWhere(bool test(E element)); 334 void retainWhere(bool test(E element));
284 335
285 /** 336 /**
286 * Returns a new list containing the objects 337 * Returns a new list containing the objects from [start] inclusive to [end]
287 * from [start] inclusive to [end] exclusive. 338 * exclusive.
339 *
340 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
341 * colors.sublist(1, 3); // ['green', 'blue']
288 * 342 *
289 * If [end] is omitted, the [length] of `this` is used. 343 * If [end] is omitted, the [length] of `this` is used.
290 * 344 *
345 * colors.sublist(1); // ['green', 'blue', 'orange', 'pink']
346 *
291 * An error occurs if [start] is outside the range `0` .. `length` or if 347 * An error occurs if [start] is outside the range `0` .. `length` or if
292 * [end] is outside the range `start` .. `length`. 348 * [end] is outside the range `start` .. `length`.
293 */ 349 */
294 List<E> sublist(int start, [int end]); 350 List<E> sublist(int start, [int end]);
295 351
296 /** 352 /**
297 * Returns an [Iterable] that iterates over the objects in the range 353 * Returns an [Iterable] that iterates over the objects in the range
298 * [start] inclusive to [end] exclusive. 354 * [start] inclusive to [end] exclusive.
299 * 355 *
300 * An error occurs if [end] is before [start]. 356 * An error occurs if [end] is before [start].
301 * 357 *
302 * An error occurs if the [start] and [end] are not valid ranges at the time 358 * An error occurs if the [start] and [end] are not valid ranges at the time
303 * of the call to this method. The returned [Iterable] behaves like 359 * of the call to this method. The returned [Iterable] behaves like
304 * `skip(start).take(end - start)`. That is, it does not throw exceptions 360 * `skip(start).take(end - start)`. That is, it does not throw exceptions
305 * if `this` changes size. 361 * if `this` changes size.
306 * 362 *
307 * Example: 363 * List<String> colors = ['red', 'green', 'blue', 'orange', 'pink'];
308 * 364 * Iterable<String> range = colors.getRange(1, 4);
309 * var list = [1, 2, 3, 4, 5]; 365 * range.join(', '); // 'green, blue, orange'
310 * var range = list.getRange(1, 4); 366 * colors.length = 3;
311 * print(range.join(', ')); // => 2, 3, 4 367 * range.join(', '); // 'green, blue'
312 * list.length = 3;
313 * print(range.join(', ')); // => 2, 3
314 */ 368 */
315 Iterable<E> getRange(int start, int end); 369 Iterable<E> getRange(int start, int end);
316 370
317 /** 371 /**
318 * Copies the objects of [iterable], skipping [skipCount] objects first, 372 * Copies the objects of [iterable], skipping [skipCount] objects first,
319 * into the range [start] inclusive to [end] exclusive of `this`. 373 * into the range [start] inclusive to [end] exclusive of `this`.
320 * 374 *
375 * List<int> list1 = [1, 2, 3, 4];
376 * List<int> list2 = [5, 6, 7, 8, 9];
377 * // Copies the 4th and 5th items in list2 as the 2nd and 3rd items
378 * // of list1.
379 * list1.setRange(1, 3, list2, 3);
380 * list1.join(', '); // '1, 8, 9, 4'
381 *
321 * If [start] equals [end] and [start]..[end] represents a legal range, this 382 * If [start] equals [end] and [start]..[end] represents a legal range, this
322 * method has no effect. 383 * method has no effect.
323 * 384 *
324 * An error occurs if [start]..[end] is not a valid range for `this`. 385 * An error occurs if [start]..[end] is not a valid range for `this`.
325 * An error occurs if the [iterable] does not have enough objects after 386 * An error occurs if the [iterable] does not have enough objects after
326 * skipping [skipCount] objects. 387 * skipping [skipCount] objects.
327 * 388 *
328 * Example:
329 *
330 * var list = [1, 2, 3, 4];
331 * var list2 = [5, 6, 7, 8, 9];
332 * list.setRange(1, 3, list2, 3);
333 * print(list); // => [1, 8, 9, 4]
334 */ 389 */
335 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]); 390 void setRange(int start, int end, Iterable<E> iterable, [int skipCount = 0]);
336 391
337 /** 392 /**
338 * Removes the objects in the range [start] inclusive to [end] exclusive. 393 * Removes the objects in the range [start] inclusive to [end] exclusive.
339 * 394 *
340 * An error occurs if [start]..[end] is not a valid range for `this`. 395 * An error occurs if [start]..[end] is not a valid range for `this`.
341 * Throws an [UnsupportedError] if this is a fixed-length list. 396 * Throws an [UnsupportedError] if this is a fixed-length list.
342 */ 397 */
343 void removeRange(int start, int end); 398 void removeRange(int start, int end);
344 399
345 /** 400 /**
346 * Sets the objects in the range [start] inclusive to [end] exclusive 401 * Sets the objects in the range [start] inclusive to [end] exclusive
347 * to the given [fillValue]. 402 * to the given [fillValue].
348 * 403 *
349 * An error occurs if [start]..[end] is not a valid range for `this`. 404 * An error occurs if [start]..[end] is not a valid range for `this`.
350 */ 405 */
351 void fillRange(int start, int end, [E fillValue]); 406 void fillRange(int start, int end, [E fillValue]);
352 407
353 /** 408 /**
354 * Removes the objects in the range [start] inclusive to [end] exclusive 409 * Removes the objects in the range [start] inclusive to [end] exclusive
355 * and replaces them with the contents of the [iterable]. 410 * and replaces them with the contents of the [iterable].
356 * 411 *
412 * List<int> list = [1, 2, 3, 4];
413 * list.replaceRange(1, 3, [6, 7]);
414 * list.join(', '); // '1, 6, 7, 4'
415 *
357 * An error occurs if [start]..[end] is not a valid range for `this`. 416 * An error occurs if [start]..[end] is not a valid range for `this`.
358 *
359 * Example:
360 *
361 * var list = [1, 2, 3, 4, 5];
362 * list.replaceRange(1, 3, [6, 7, 8, 9]);
363 * print(list); // [1, 6, 7, 8, 9, 4, 5]
364 */ 417 */
365 void replaceRange(int start, int end, Iterable<E> iterable); 418 void replaceRange(int start, int end, Iterable<E> iterable);
366 419
367 /** 420 /**
368 * Returns an unmodifiable [Map] view of `this`. 421 * Returns an unmodifiable [Map] view of `this`.
369 * 422 *
370 * The map uses the indices of this list as keys and the corresponding objects 423 * The map uses the indices of this list as keys and the corresponding objects
371 * as values. The `Map.keys` [Iterable] iterates the indices of this list 424 * as values. The `Map.keys` [Iterable] iterates the indices of this list
372 * in numerical order. 425 * in numerical order.
426 *
427 * List<String> words = ['fee', 'fi', 'fo', 'fum'];
428 * Map<int, String> map = words.asMap();
429 * map[0] + map[1]; // 'feefi';
430 * map.keys.toList(); // [0, 1, 2, 3]
373 */ 431 */
374 Map<int, E> asMap(); 432 Map<int, E> asMap();
375 } 433 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698