資料結構 集合

2021-10-09 12:33:27 字數 2698 閱讀 6543

乙個識別符號(集合set),可以儲存多個資料,資料不能重複。

集合與陣列的區別,集合的內容具有唯一性(全等匹配)

宣告集合

let

f70=

newset

(f71

);

集合賦值

let

f71=

;let

f70=

newset

(f71);

// 集合賦值

f70.

add(1)

;f70

.add(2

);f70.

add(1)

;f70

.add

("1");

f70.

add(

"1")

;// 使用集合

console.

log(

f70)

;// set

集合去重

let

f71=

;f71

.push

(123);

f71.

push

(234);

f71.

push

(234);

console.

log(

f71)

;//[ 123, 234, 234 ]

letf70

=new

set(

f71)

;//set

console.

log(

f70)

;

1、屬性size ,功能:輸出集合儲存資料的個數;

書寫格式: 集合.size

let

f73=

newset([

1,2,

3]);

console.

log(

f73.size)

;// 3

2、集合轉陣列
let

f71=

;f71

.push

(123);

f71.

push

(234);

f71.

push

(234);

letf70

=new

set(

f71)

;let

f72=

[...

f70]

;console.

log(

f72)

;//[ 123, 234 ]

3、集合的方法

鏈式呼叫:一行**連續書寫多個方法,由於add方法的返回值是集合,因此可以在add後面繼續新增add

f73

.add

('a').

add(

'b')

.add

('c');

console.

log(

f73)

;//set

letf74

=f73

.add

('a');

console.

log(

f74)

;//set

刪除集合裡的內容:集合的鍵值對(鍵值就是鍵名)

let

f74=

f73.

add(

'a')

;console.

log(

f74)

;//set

f74.

delete

('a');

//小括號裡刪除的內容

console.

log(

f74)

;//set

清空集合

let

f74=

newset([

1,2,

3]);

f74.

clear()

;console.

log(

f74)

;//set {}

查詢

let

f73=

newset([

1,2,

3]);

console.

log(

f73.

has(1)

);//true

4、集合的鍵值對(鍵值就是鍵名)
for

(const item of

f74.

keys()

);for(

const item of

f74.

values()

);for(

const item of

f74.

entries()

);

資料結構 集合

陣列 陣列長度在初始化的時候就已經固定,不適合物件數量未知的情況。下圖為collection 於的部落格 下圖為map 於網路 1.介紹一下list比較常用的集合 有序,值允許重複 1 arraylist 底層實現 private static final object defaultcapacit...

集合和資料結構

類似的資料在作為集合而儲存和操作時通常可以得到更高效地處理。可使用 system.array 類或 system.collections system.collections.generic system.collections.concurrent 和 system.collections.imm...

資料結構 集合類

2 linkedlist 3 hashmap 4 hashtable arraylist初始長度為0,當第一次呼叫add後,長度變為10,當陣列首次擴容的10個空間用完需要擴容後,會第二次走grow方法來擴容 每次擴容為1.5倍 它的底層是用陣列實現的,所以查詢速度相對linkedlist要快。li...