ArrayList<G>

Реализация массива изменяемого размера c интерфейсом List .

Массив автоматически увеличивается при необходимости.

Когда использовать

Эта реализация хороша для редко изменяемых данных. Поскольку данные хранятся в массиве, эта структура не подходит для сильно изменяемых данных. альтернативная реализация см. LinkedList .

List Example

using Gee;

void main () {
    var list = new ArrayList<int> ();
    list.add (1);
    list.add (2);
    list.add (5);
    list.add (4);
    list.insert (2, 3);
    list.remove_at (3);
    foreach (int i in list) {
        stdout.printf ("%d\n", i);
    }
    list[2] = 10;                       // same as list.set (2, 10)
    stdout.printf ("%d\n", list[2]);    // same as list.get (2)
}

Compile and Run

$ valac --pkg gee-0.8 gee-list.vala
$ ./gee-list

You can use any type fitting into the size of a pointer (e.g. int, bool, reference types) directly as generic type argument: <bool>, <int>, <string>, <MyObject>. Other types must be "boxed" by appending a question mark: <float?>, <double?>, <MyStruct?>. The compiler will tell you this if necessary.

Content:

Properties:

  • public EqualDataFunc<G> equal_func { get; } The elements' equality testing function.

  • public override bool read_only { get; } Specifies whether this collection can change - i.e. wheather add, remove etc. are legal operations.

  • public override int size { get; } The number of items in this collection.

Creation methods:

Methods:

  • public override bool @foreach (ForallFunc<G> f)

  • public override G @get (int index) Returns the item at the specified index in this list.

  • public override void @set (int index, G item) Sets the item at the specified index in this list.

  • public override bool add (G item) Adds an item to this collection. Must not be called on read-only collections.

  • public bool add_all (Collection<G> collection)

  • public override BidirListIterator<G> bidir_list_iterator () Returns a BidirListIterator that can be used for iteration over this list.

  • public override void clear () Removes all items from this collection. Must not be called on read-only collections.

  • public override bool contains (G item) Determines whether this collection contains the specified item.

  • public override int index_of (G item) Returns the index of the first occurence of the specified item in this list.

  • public override void insert (int index, G item) Inserts an item into this list at the specified position.

  • public override Iterator<G> iterator () Returns a Iterator that can be used for simple iteration over a collection.

  • public override ListIterator<G> list_iterator () Returns a ListIterator that can be used for iteration over this list.

  • public override bool remove (G item) Removes the first occurence of an item from this collection. Must not be called on read-only collections.

  • public override G remove_at (int index) Removes the item at the specified index of this list.

  • public override List<G>? slice (int start, int stop) Returns a slice of this list.

Полный список коллекций см здесь.

Last updated