Options
All
  • Public
  • Public/Protected
  • All
Menu

Class List<T>

Hierarchy

  • List

NOTE: For 2.0 the constructor argument has changed. List now optionally accepts a collection, and only checks types in TypeScript.

An ordered iterable collection. In TypeScript it is a generic class that enforces at compile-time the type of elements that may be added to the List.

An example usage:

  var list = new go.List();  // or in TypeScript: new go.List<go.Point>();
  list.add(new go.Point(0, 0));
  list.add(new go.Point(20, 10));
  list.add(new go.Point(10, 20));
  // now list.length === 3
  // and list.elt(1) instanceof go.Point

You can iterate over the items in a List:

  var it = aList.iterator;
  while (it.next()) {
    console.log("#" + it.key + " is " + it.value);
  }

Or:

  aList.each(function(val) {
    console.log(val);
  });

The key will range from zero to count-1.

For convenience this GoJS List class has synonyms for the following methods and property:

The constructor now takes an optional Iterable or Array argument that provides the initial elements for the new List.

Note that GoJS iteration is quite different than ES6 iteration, so that functionality has not been made somewhat compatible. These collection classes were defined in GoJS before the ES6 collection classes were proposed.

Type parameters

  • T

Implements

Index

Constructors

constructor

  • There are two possible constructors:

    new go.List(), for JavaScript

    new go.List<type>() for TypeScript, to enforce type checking.

    Typical usage would be something like:

      var list = new go.List();  // keep a list of GraphObjects
    

    Parameters

    • Optional coll: Iterable<T> | Array<T>

      an optional collection of items to add.

    Returns List

Properties

Read-only count : number

  • This read-only property is the length of the List.

Read-only iterator : Iterator<T>

  • Gets an object that you can use for iterating over the List. The key will be an integer from zero to the count-1. The value will be the item at that index in the list. Typical usage:

      var it = aList.iterator;
      while (it.next()) {
        . . . "index: " + it.key + " value: " + it.value . . .
      }
    

Read-only iteratorBackwards : Iterator<T>

  • Gets an object that you can use for iterating over the List in backwards order. The key will be an integer from count-1 to zero. The value will be the item at that index in the list. The list is not modified by traversing in reverse order. Typical usage:

      var it = aList.iteratorBackwards;
      while (it.next()) {
        . . . 'key: ' + it.key + ' value: ' + it.value . . .
      }
    

Read-only length : number

  • This read-only property is the length of the List, a synonym for the count property.

Read-only size : number

  • This read-only property is the length of the List.

Methods

add

  • add(val: T): List<T>
  • Adds a given value to the end of the List.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • val: T

    Returns List<T>

    This modified List.

addAll

  • Adds all of the values of a collection to the end of this List.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • coll: Iterable<T> | Array<T>

      the collection of items to add.

    Returns List<T>

    This modified List.

Virtual all

  • all(pred: function(a: T): boolean): boolean
  • This is true if all invocations of the given predicate on items in the collection are true.

    Call the given predicate on each item in the collection. As soon as a call returns false, this returns false. Otherwise this returns true. For an empty collection this returns true.

    since

    1.4

    Parameters

    • pred: function(a: T): boolean

      This function must not have any side-effects.

    Returns boolean

    True if all predicate calls are true; false otherwise.

Virtual any

  • any(pred: function(a: T): boolean): boolean
  • This is true if any invocation of the given predicate on items in the collection is true.

    Call the given predicate on each item in the collection. As soon as a call returns true, this returns true. Otherwise this returns false. For an empty collection this returns false.

    since

    1.4

    Parameters

    • pred: function(a: T): boolean

      This function must not have any side-effects.

    Returns boolean

    True if any predicate call is true; false otherwise.

clear

  • clear(): void
  • Clears the List. This sets the count to zero.

    Be careful not to call this method while iterating over the collection.

    Returns void

contains

  • contains(val: T): boolean
  • Returns whether the given value is in this List.

    Parameters

    • val: T

      The value to check.

    Returns boolean

    Whether or not the value is contained within the List.

Virtual copy

  • Makes a shallow copy of this List. The values are not copied, so if they are objects they may continue to be shared with the original List.

    Returns List<T>

    The new List with the same elements.

delete

  • delete(val: T): boolean
  • Removes a given value (if found) from the List.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • val: T

      The value to remove.

    Returns boolean

    true if the value was found and removed, false otherwise.

Virtual each

  • each(func: function(a: T): void): List<T>
  • Call the given function on each item in the collection.

    since

    1.4

    Parameters

    • func: function(a: T): void

      This function must not modify the collection.

    Returns List<T>

    This List itself

elt

  • elt(i: number): T
  • Returns the element at the given index.

    Parameters

    • i: number

      int The index of the element to return.

    Returns T

    the value at the given index.

first

  • first(): T | null
  • Returns the first item in the list, or null if there is none.

    Returns T | null

    This returns null if there are no items in the list.

get

  • get(i: number): T
  • Returns the element at the given index.

    Parameters

    • i: number

      int The index of the element to return.

    Returns T

    the value at the given index.

has

  • has(val: T): boolean
  • Returns whether the given value is in this List.

    Parameters

    • val: T

      The value to check.

    Returns boolean

    Whether or not the value is contained within the List.

indexOf

  • indexOf(val: T): number
  • Returns the index of the given value if it is in this List.

    Parameters

    • val: T

      The value to check.

    Returns number

    int returns -1 if the value is not in this list.

insertAt

  • insertAt(i: number, val: T): void
  • Insert a value before the index i.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • i: number

      int The index to insert before.

    • val: T

      The value to insert.

    Returns void

last

  • last(): T | null
  • Returns the last item in the list, or null if these is none.

    since

    1.5

    Returns T | null

    This returns null if there are no items in the list.

pop

  • pop(): T | null
  • Returns the last item in the list and removes it from the list, or just return null if these is none. Use add to push an item onto the end of the list. Use last to get the last item without reducing the length of the list.

    since

    1.5

    Returns T | null

    This returns null if there are no items in the list.

push

  • push(val: T): void
  • Adds a given value to the end of the List.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • val: T

    Returns void

remove

  • remove(val: T): boolean
  • Removes a given value (if found) from the List.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • val: T

      The value to remove.

    Returns boolean

    true if the value was found and removed, false otherwise.

removeAt

  • removeAt(i: number): void
  • Removes a value at a given index from the List.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • i: number

      int The index to remove.

    Returns void

removeRange

  • removeRange(from: number, to: number): List<T>
  • Removes a range of values from the List, given both the starting and the ending zero-based indexes. For example,

    list.removeRange(2, 4)
    

    will remove elements 2, 3, and 4 from the list. If there were two or fewer elements in the list to begin with, the list is unchanged. If from is greater than to, the list is unchanged. If from is greater than or equal to the length, the list is unchanged. If to is less than zero, the list is unchanged.

    Be careful not to call this method while iterating over the collection.

    Parameters

    • from: number

      int The starting index of the range to remove, inclusive; negative values are treated as zero

    • to: number

      int The ending index of the range to remove, inclusive; values greater than the length of the list are treated as referring to the last element

    Returns List<T>

    This modified List

reverse

  • reverse(): List<T>
  • Reverse the order of items in this List.

    Returns List<T>

    This modified List.

set

  • set(i: number, val: T): void
  • Set the element at the given index to a given value.

    Parameters

    • i: number

      int The index of the element to set.

    • val: T

      The value to set at the index.

    Returns void

setElt

  • setElt(i: number, val: T): void
  • Set the element at the given index to a given value.

    Parameters

    • i: number

      int The index of the element to set.

    • val: T

      The value to set at the index.

    Returns void

sort

  • sort(sortfunc: function(a: T, b: T): number): List<T>
  • Sort the List according to a comparison function.

    Parameters

    • sortfunc: function(a: T, b: T): number

      This function is passed two items in the list. It should return zero if they are equal, less than zero if the first value should come before the second value, or greater than zero if the first value should come after the second value.

    Returns List<T>

    This modified List.

toArray

  • toArray(): Array<T>
  • Produces a JavaScript Array from the contents of this List.

    Returns Array<T>

    A copy of the List in Array form.

toSet

  • toSet(): Set<T>
  • Converts the List to a Set. The count of the resulting Set may be less than the count of this List if any duplicates were removed.

    Returns Set<T>

    A copy of the contents of this List, but with duplicates removed and ordering lost.