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

  • 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
    

    Type parameters

    • T

    Parameters

    • Optional coll: Iterable<T> | T[]

      an optional collection of items to add.

    Returns List<T>

Properties

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

  • 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 . . .
    }
  • 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 . . .
    }
  • This read-only property is the length of the List, a synonym for the count property.

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

Methods

  • 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.

  • 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> | T[]

      the collection of items to add.

    Returns List<T>

    This modified List.

  • all(pred: (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: (a: T) => boolean

      This function must not have any side-effects.

        • (a: T): boolean
        • Parameters

          • a: T

          Returns boolean

    Returns boolean

    True if all predicate calls are true; false otherwise.

  • any(pred: (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: (a: T) => boolean

      This function must not have any side-effects.

        • (a: T): boolean
        • Parameters

          • a: T

          Returns boolean

    Returns boolean

    True if any predicate call is true; false otherwise.

  • 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(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.

  • 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(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.

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

    since

    1.4

    Parameters

    • func: (a: T) => void

      This function must not modify the collection.

        • (a: T): void
        • Parameters

          • a: T

          Returns void

    Returns List<T>

    This List itself

  • 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(): T
  • Returns the first item in the list, or null if there is none.

    Returns T

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

  • 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(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(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(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(): T
  • Returns the last item in the list, or null if these is none.

    since

    1.5

    Returns T

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

  • pop(): T
  • 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

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

  • 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(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(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(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 the order of items in this List.

    Returns List<T>

    This modified List.

  • 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(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(sortfunc: (a: T, b: T) => number): List<T>
  • Sort the List according to a comparison function.

    Parameters

    • sortfunc: (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.

        • (a: T, b: T): number
        • Parameters

          • a: T
          • b: T

          Returns number

    Returns List<T>

    This modified List.

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

    Returns T[]

    A copy of the List in Array form.

  • 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.