harmony 鸿蒙@ohos.util.Stack (Linear Container Stack)

2022-08-09 浏览 (851)

@ohos.util.Stack (Linear Container Stack)

Stack is implemented based on the array data structure. It follows the principle Last Out First In (LOFI) and supports data insertion and removal at one end.

Unlike Queue, which is implemented based on the queue data structure and supports insertion at one end and removal at the other end, Stack supports insertion and removal at the same end.

Recommended use case: Use Stack in LOFI scenarios.

This topic uses the following to identify the use of generics:

  • T: Type

NOTE

The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import Stack from '@ohos.util.Stack';  

Stack

Attributes

System capability: SystemCapability.Utils.Lang

NameTypeReadableWritableDescription
lengthnumberYesNoNumber of elements in a stack (called container later).

constructor

constructor()

A constructor used to create a Stack instance.

System capability: SystemCapability.Utils.Lang

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200012The Stack's constructor cannot be directly invoked.

Example

let stack : Stack<number|string|Object> = new Stack();

push

push(item: T): T

Adds an element at the top of this container.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
itemTYesTarget element.

Return value

TypeDescription
TElement added.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The push method cannot be bound.

Example

class C1 {
  name: string = ""
  age: string = ""
}
let stack : Stack<number|string|C1> = new Stack();
let result = stack.push("a");
let result1 = stack.push(1);
let b = [1, 2, 3];
let result2 = stack.push(b);
let c : C1  = {name : "Dylon", age : "13"};
let result3 = stack.push(c);

pop

pop(): T

Removes the top element from this container.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
TElement removed.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The pop method cannot be bound.

Example

let stack : Stack<number> = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
stack.push(4);
let result = stack.pop();

peek

peek(): T

Obtains the top element of this container.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
TElement obtained.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The peek method cannot be bound.

Example

let stack : Stack<number> = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.peek();

locate

locate(element: T): number

Obtains the index of the first occurrence of the specified element in this container.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
elementTYesTarget element.

Return value

TypeDescription
numberReturns the position index if obtained; returns -1 otherwise.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The locate method cannot be bound.

Example

let stack : Stack<number> = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(2);
let result = stack.locate(2);

forEach

forEach(callbackFn: (value: T, index?: number, stack?: Stack<T>) => void, thisArg?: Object): void

Uses a callback to traverse the elements in this container and obtain their position indexes.

System capability: SystemCapability.Utils.Lang

Parameters

NameTypeMandatoryDescription
callbackFnfunctionYesCallback invoked to traverse the elements in the container.
thisArgObjectNoValue of this to use when callbackFn is invoked. The default value is this instance.

callbackFn

NameTypeMandatoryDescription
valueTYesValue of the element that is currently traversed.
indexnumberNoPosition index of the element that is currently traversed. The default value is 0.
stackStack<T>NoInstance that calls the forEach API. The default value is this instance.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The forEach method cannot be bound.

Example

let stack : Stack<number> = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
stack.forEach((value : number, index ?: number) :void => {
  console.log("value:" + value, "index:" + index);
});

isEmpty

isEmpty(): boolean

Checks whether this container is empty (contains no elements).

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
booleanReturns true if the container is empty; returns false otherwise.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The isEmpty method cannot be bound.

Example

let stack : Stack<number> = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);
let result = stack.isEmpty();

[Symbol.iterator]

[Symbol.iterator](): IterableIterator<T>

Obtains an iterator, each item of which is a JavaScript object.

System capability: SystemCapability.Utils.Lang

Return value

TypeDescription
IterableIterator<T>Iterator obtained.

Error codes

For details about the error codes, see Utils Error Codes.

IDError Message
10200011The Symbol.iterator method cannot be bound.

Example

let stack : Stack<number> = new Stack();
stack.push(2);
stack.push(4);
stack.push(5);
stack.push(4);

// Method 1:
while(!stack.isEmpty()) {
  // Service logic
  let item = stack.pop()
  console.log("value:" + item);
}

// Method 2:
let iter = stack[Symbol.iterator]();
let temp: IteratorResult<number> = iter.next().value;
while(temp != undefined) {
  console.log("value:" + temp);
  temp = iter.next().value;
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙APIs

harmony 鸿蒙System Common Events (To Be Deprecated Soon)

harmony 鸿蒙System Common Events

harmony 鸿蒙API Reference Document Description

harmony 鸿蒙Enterprise Device Management Overview (for System Applications Only)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager)

harmony 鸿蒙@ohos.distributedBundle (Distributed Bundle Management)

harmony 鸿蒙@ohos.bundle (Bundle)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/4bf4ee12740540129009941cc87dfce5