Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Iterator_by_Ilalov_ts/CollectionImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {ICollection} from "./ICollection";
import {IIterator} from "./IIterator";
import {IteratorImpl} from "./IteratorImpl";

//Реализация коллекции
export class CollectionImpl implements ICollection{
private items: any[] = [];

public getIterator(): IIterator<any> {
return new IteratorImpl(this);
}

public addItem(item: any): void {
this.items.push(item);
}

public getItems(): any[] {
return this.items;
}

public getLength(): number {
return this.items.length;
}
}
13 changes: 13 additions & 0 deletions Iterator_by_Ilalov_ts/ICollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {IIterator} from "./IIterator";

export interface ICollection {

//Вызов итератора
getIterator(): IIterator<any>
//Добавление элемента
addItem(item): void
//Получение всех элементов
getItems(): any[]
//Получение количества элементов
getLength(): number
}
6 changes: 6 additions & 0 deletions Iterator_by_Ilalov_ts/IIterator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface IIterator<T> {
//Получить текущий элемент и перейти к следующему
getNext(): T;
//Проверить наличие следующего элемента
hasNext(): boolean;
}
20 changes: 20 additions & 0 deletions Iterator_by_Ilalov_ts/IteratorImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {IIterator} from "./IIterator";
import {ICollection} from "./ICollection";

//Реализация итератора
export class IteratorImpl implements IIterator<any> {
private collection: ICollection;
private index: number = 0;

constructor(collection: ICollection) {
this.collection = collection;
}

public getNext(): any {
return this.collection.getItems()[this.index++];
}

public hasNext(): boolean {
return this.index < this.collection.getLength();
}
}
14 changes: 14 additions & 0 deletions Iterator_by_Ilalov_ts/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {CollectionImpl} from "./CollectionImpl";

const collection = new CollectionImpl();
//Добавление элементов в коллекцию
collection.addItem(1);
collection.addItem('Two');
collection.addItem(true);
collection.addItem([4, 5]);

const iterator = collection.getIterator();

while (iterator.hasNext()) {
console.log(iterator.getNext());
}