生活随笔
收集整理的這篇文章主要介紹了
1_01李婉玲_数据结构_1012
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Day03作業 數據結構
概述 基本類型和引用類型
基本類型:String Boolean Number Null Undefined Symbol Bigint。
引用類型:Object(存儲屬性和屬性值) Function。
數組(Array ):有序的值的列表。 集合(Set ):唯一值的集合。 特殊集合(WeakSet ):特殊Set。 映射(Map ):鍵值對。 特殊映射(WeakMap ):特殊Map。
區別:
(內存存儲)基本類型存儲在棧(Stack)里面;引用類型在棧上存儲對堆上數據的引用(指針),在堆(Heap)上存儲引用類型本身的數據。 (清理方式)基本類型函數和方法執行完就清理內存;引用類型采用垃圾回收機制。
對象 存儲屬性和屬性值
定義對象:
let person
= new Object ( ) ;
person
. name
= 'liwanling' ;
person
. age
= 18 ;
console
. log ( person
. name
)
let person1
= { } ;
person1
. name
= 'liwanling' ;
person1
. age
= 18 ;
console
. log ( person1
. name
) ;
let person2
= { } ;
let person2
= { name
: 'liwanling' , age
: 18 , getUserName
: function ( ) { }
} ;
console
. log ( person2
. name
) ;
console
. log ( typeof person2
) ;
數組 有序的值的列表
(與其他編程語言相比)區別:
數組的每個槽位可以存儲任意類型的數據。 動態大小 —— 影響性能。
定義數組:(聲明)
const oArray
= new Array ( ) ;
const oArray
= [ ] ;
console
. log ( typeof oArray
) ;
初始化:
const heroes
= [ ] ;
console
. log ( heroes
[ 0 ] ) ; const heroes
= [ ] ;
heroes
[ 0 ] = '蝙蝠俠' ;
heroes
[ 1 ] = '神奇女俠' ;
heroes
[ 2 ] = '閃電俠' ;
heroes
[ 5 ] = '水行俠' ;
console
. log ( heroes
) ; const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
console
. log ( avengers
) ;
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
delete avengers
[ 0 ] ;
console
. log ( avengers
) ;
let person2
= { name
: 'liwanling' , age
: 18 ,
} ;
const a
= [ 0 , undefined
, '123112' , null , person2
] ;
console
. log ( a
) ;
解構數組:將數組中的元素取出來賦值給變量。
const array1
= [ 1 , 2 , 3 ] ;
let a
= array1
[ 0 ] ;
let b
= array1
[ 1 ] ;
let c
= array1
[ 2 ] ; const [ a
, b
, c
] = [ 1 , 2 , 3 ] ;
console
. log ( `a= ${ a} ,b= ${ b} ,c= ${ c} ` ) ; const [ a
, b
] = [ 1 , 2 , 3 ] ;
console
. log ( a
) ;
數組的屬性和方法:
length屬性:數組的長度。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
console
. log ( avengers
. length
) ;
avengers
. length
= 1 ;
console
. log ( avengers
) ;
avengers
. length
= 5 ;
console
. log ( avengers
) ;
pop()方法:刪掉數組中最后一個元素。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
console
. log ( avengers
. pop ( ) ) ;
console
. log ( avengers
) ;
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
delete avengers
[ 0 ] ;
console
. log ( avengers
) ;
pop()方法和delete運算符的區別:pop數組元素個數減少,delete運算符元素個數沒變。
push()方法:將新值添加到數組的末尾。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
avengers
. push ( '蝙蝠俠' ) ;
console
. log ( avengers
) ;
shift()方法:刪除數組中的第一個元素。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
avengers
. shift ( ) ;
console
. log ( avengers
) ;
console
. log ( avengers
. shift ( ) ) ;
unshift()方法:將新值添加到數組的開頭。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
avengers
. unshift ( '小超人' ) ;
console
. log ( avengers
) ;
console
. log ( avengers
. unshift ( ) ) ;
concat()方法:數組合并。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
const heroes
= [ '蝙蝠俠' , '神奇女俠' , '閃電俠' , '水行俠' ] ;
const oArray
= avengers
. concat ( heroes
) ;
console
. log ( avengers
) ;
console
. log ( oArray
) ;
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
const heroes
= [ '蝙蝠俠' , '神奇女俠' , '閃電俠' , '水行俠' ] ;
const oArray
= [ ... avengers
, ... heroes
] ;
console
. log ( oArray
) ;
join()方法:數組變成組合了數組所有元素的字符串。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
const a
= avengers
. join ( `&` ) ;
console
. log ( a
) ;
slice()方法:從原始數組中切掉一片,從而創建一個子數組。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
const b
= avengers
. slice ( 2 , 3 ) ;
console
. log ( b
) ;
console
. log ( avengers
) ;
splice()方法:從一個數組中刪除元素,然后將新元素插入在被刪除的元素的位置上。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
const c
= avengers
. splice ( 2 , 2 , [ 'liwanling' , 'li' ] ) ;
const cc
= avengers
. splice ( 2 , 2 , 'liwanling' , 'li' ) ;
const ccc
= avengers
. splice ( 2 , 2 , 'liwanling' , 'li' , 'wang' ) ;
console
. log ( c
) ;
console
. log ( cc
) ;
console
. log ( ccc
) ;
console
. log ( avengers
) ;
reverse()方法:反轉數組中元素的次序(永久性改變)。
const d
= [ 'a' , 'b' , 'c' , 'd' ] ;
const e
= d
. reverse ( ) ;
console
. log ( e
, d
) ;
sort()方法:對數組中的元素按字母順序進行排序(永久性改變)。
const f
= [ 1 , 2 , 10 , 1 , 2 ] ;
const g
= f
. sort ( ) ;
console
. log ( g
, f
) ;
indexOf():檢測數組中是否包含一個特定值,如果找到了,就返回該值在數組中第一次出現的索引號,否則,就返回-1。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
const a1
= avengers
. indexOf ( '美國隊長1' ) ;
console
. log ( a1
) ;
includes():檢測數組中是否包含特定值,如果找到了,就返回true,否則就返回false。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
const a2
= avengers
. includes ( '美國隊長2' ) ;
console
. log ( a2
) ;
多維數組:
作用:坐標系統(二維數組)以及復雜算法。
舉例:
const ma
= [ [ 1 , 2 ] , [ 3 , 4 ] ] ;
console
. log ( ma
[ 0 ] [ 0 ] ) ; const summer
= [ 'Jun' , 'Jul' , 'Aug' ] ;
const winter
= [ 'Dec' , 'Jan' , 'Feb' ] ;
const nested
= [ summer
, winter
] ;
console
. log ( nested
) ;
const summer
= [ 'Jun' , 'Jul' , 'Aug' ] ;
const winter
= [ 'Dec' , 'Jan' , 'Feb' ] ;
const flat
= [ ... summer
, ... winter
] ;
console
. log ( flat
) ;
數組去重:(Set和數組的轉換)
const a
= [ 1 , 2 , 12 , 1 , 2 , 3 , 4 , 5 ] ;
const b
= new Set ( a
) ;
console
. log ( b
) ;
const c
= [ ... b
] ;
console
. log ( c
) ;
集合 唯一值的集合
定義集合:
const list
= new Set ( ) ;
list
. add ( 1 ) ;
list
. add ( 2 ) . add ( 3 ) . add ( 4 ) . add ( 5 ) ;
list
. add ( 5 ) ;
console
. log ( list
) ;
const list
= new Set ( [ 1 , 2 , 3 , 4 , 5 ] ) ;
console
. log ( list
) ;
const c
= new Set ( 'Hello' ) ;
console
. log ( c
) ;
const list4
= new Set ( ) . add ( 'the' ) . add ( 'quick' ) . add ( 'brown' ) . add ( 'fox' ) ;
console
. log ( list4
) ;
屬性和方法:
size屬性:獲取集合中值的數目。(只讀屬性)
const list4
= new Set ( ) . add ( 'the' ) . add ( 'quick' ) . add ( 'brown' ) . add ( 'fox' ) ;
console
. log ( list4
. size
) ; const list4
= new Set ( ) . add ( 'the' ) . add ( 'quick' ) . add ( 'brown' ) . add ( 'fox' ) ;
list4
. size
= 3 ;
console
. log ( list4
) ;
has()方法:用于檢測一個值是否在集合中,返回true或者false。
const list4
= new Set ( ) . add ( 'the' ) . add ( 'quick' ) . add ( 'brown' ) . add ( 'fox' ) ;
console
. log ( list4
. has ( 'brown' ) ) ;
delete()方法:從集合中刪除一個值。
const list4
= new Set ( ) . add ( 'the' ) . add ( 'quick' ) . add ( 'brown' ) . add ( 'fox' ) ;
list4
. delete ( 'the' ) ;
console
. log ( list4
) ;
clear()方法:刪掉集合中的所有值。
const list4
= new Set ( ) . add ( 'the' ) . add ( 'quick' ) . add ( 'brown' ) . add ( 'fox' ) ;
list4
. clear ( ) ;
console
. log ( list4
) ;
Set和數組的轉換:數組去重也用到了。擴展運算符 Array.from() 方法
const list4
= new Set ( ) . add ( 'the' ) . add ( 'quick' ) . add ( 'brown' ) . add ( 'fox' ) ;
const oArray
= [ ... list4
] ;
console
. log ( oArray
) ;
const list4
= new Set ( ) . add ( 'the' ) . add ( 'quick' ) . add ( 'brown' ) . add ( 'fox' ) ;
const oArray
= Array
. from ( list4
) ;
console
. log ( oArray
) ;
集合 唯一值的集合
當對象添加到Set中時,只要Set存在,它們就會一直存儲在Set中,即使對對象的原始引用被刪除了依然如此。用技術術語來說,就是對象阻止被垃圾回收,而這會導致內存泄漏。 WeakSet 通過垃圾回收任何引用原始引用已經被刪掉的“死對象”,從而可以避免這種情況。 WeakSet 只能添加非基本類型數據,否則會拋出一個類型錯誤(TypeError)。
let array1
= [ 1 , 2 , 3 ] ;
let array2
= [ 3 , 4 , 5 ] ;
const strong
= new Set ( ) . add ( array1
) . add ( array2
) ;
console
. log ( strong
. has ( array1
) ) ;
array1
= null ;
array2
= null ;
array3
= [ ... strong
] [ 0 ] ;
array4
= [ ... strong
] [ 1 ] ;
console
. log ( array3
) ;
console
. log ( array4
) ;
let array1
= [ 1 , 2 , 3 ] ;
let array2
= [ 3 , 4 , 5 ] ;
const weak
= new WeakSet ( ) . add ( array1
) . add ( array2
) ;
console
. log ( weak
) ;
array1
= null ;
array2
= null ;
const array3
= [ ... weak
] [ 0 ] ;
const array4
= [ ... weak
] [ 1 ] ;
console
. log ( array3
) ;
console
. log ( array4
) ;
Map 存儲鍵值對列表
創建Map:
const romanNumerals
= new Map ( ) ;
romanNumerals
. set ( 1 , 'I' ) ;
romanNumerals
. set ( 2 , 'II' ) . set ( 3 , 'III' ) . set ( 4 , 'IV' ) . set ( 5 , 'V' ) ;
console
. log ( romanNumerals
) ;
方法和屬性:
size屬性:獲取鍵和值的數量。(只讀屬性)
const romanNumerals
= new Map ( ) ;
romanNumerals
. set ( 1 , 'I' ) ;
romanNumerals
. set ( 2 , 'II' ) . set ( 3 , 'III' ) . set ( 4 , 'IV' ) . set ( 5 , 'V' ) ;
console
. log ( romanNumerals
. size
) ;
get(key):通過鍵獲取值。
const romanNumerals
= new Map ( ) ;
romanNumerals
. set ( 1 , 'I' ) ;
romanNumerals
. set ( 2 , 'II' ) . set ( 3 , 'III' ) . set ( 4 , 'IV' ) . set ( 5 , 'V' ) ;
console
. log ( romanNumerals
. get ( 3 ) ) ;
has(key):檢測一個特定鍵是否在映射中。
const romanNumerals
= new Map ( ) ;
romanNumerals
. set ( 1 , 'I' ) ;
romanNumerals
. set ( 2 , 'II' ) . set ( 3 , 'III' ) . set ( 4 , 'IV' ) . set ( 5 , 'V' ) ;
console
. log ( romanNumerals
. has ( 5 ) ) ;
delete(key):從映射中刪除一個鍵值對。
const romanNumerals
= new Map ( ) ;
romanNumerals
. set ( 1 , 'I' ) ;
romanNumerals
. set ( 2 , 'II' ) . set ( 3 , 'III' ) . set ( 4 , 'IV' ) . set ( 5 , 'V' ) ;
romanNumerals
. delete ( 5 ) ;
console
. log ( romanNumerals
) ;
clear():從映射中刪除所有鍵值對。
const romanNumerals
= new Map ( ) ;
romanNumerals
. set ( 1 , 'I' ) ;
romanNumerals
. set ( 2 , 'II' ) . set ( 3 , 'III' ) . set ( 4 , 'IV' ) . set ( 5 , 'V' ) ;
romanNumerals
. clear ( ) ;
console
. log ( romanNumerals
) ;
Map轉換為數組:
const romanNumerals
= new Map ( ) ;
romanNumerals
. set ( 1 , 'I' ) . set ( 2 , 'II' ) . set ( 3 , 'III' ) . set ( 4 , 'IV' ) . set ( 5 , 'V' ) ;
const oArray1
= [ ... romanNumerals
] ;
const oArray2
= Array
. from ( romanNumerals
) ;
console
. log ( oArray1
) ;
console
. log ( oArray2
) ;
WeakMap
鍵不能是基本數據類型的值,并且在對原始對象的引用被刪除時,垃圾回收會自動刪除所有死條目。
let array5
= [ 1 , 'I' ] ;
let array6
= [ 2 , 'II' ] ;
strong1
= new Map ( ) . set ( array5
) . set ( array6
) ;
console
. log ( strong1
. has ( array5
) ) ;
array5
= null ;
array6
= null ;
array7
= [ ... strong1
] [ 0 ] ;
array8
= [ ... strong1
] [ 1 ] ;
console
. log ( array7
) ;
console
. log ( array8
) ; let array9
= [ 1 , 'I' ] ;
let array10
= [ 2 , 'II' ] ;
weak1
= new WeakMap ( ) . set ( array9
) . set ( array10
) ;
console
. log ( weak1
. has ( array9
) ) ;
array9
= null ;
array10
= null ;
array11
= [ ... Weak1
] [ 0 ] ;
array12
= [ ... Weak1
] [ 1 ] ;
console
. log ( array11
) ;
console
. log ( array12
) ;
總結
數據類型
基礎類型 :String Number Boolean Symbol undefined null。
引用類型 :Object/Function。
對象
創建對象的兩種方式
構造器
let oStudent
= new Object ( ) ;
對象字面量
let oStudent
= { name
: 'xaaaa' , age
: 21
} ;
oStudent
. name
Array
1.創建數組的兩種方式
構造器
const oArray
= new Array ( ) ;
數組字面量
let person1
= { } ;
2.初始化
逐個賦值
const heroes
= [ ] ;
heroes
[ 0 ] = '蝙蝠俠' ;
heroes
[ 1 ] = '神奇女俠' ;
數組字面量
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
3.刪除數組元素
delete avengers
[ 0 ] ;
4.解構數組
const [ a
, b
, c
] = [ 1 , 2 , 3 ] ;
console
. log ( `a= ${ a} ,b= ${ b} ,c= ${ c} ` ) ;
5.數組的屬性和方法
length屬性:數組的長度。
console
. log ( avengers
. length
) ;
pop()方法:刪掉數組中最后一個元素。
console
. log ( avengers
. pop ( ) ) ;
push()方法:將新值添加到數組的末尾。
avengers
. push ( '蝙蝠俠' ) ;
shift()方法:刪除數組中的第一個元素。
avengers
. shift ( ) ;
unshift()方法:將新值添加到數組的開頭。
avengers
. unshift ( '小超人' ) ;
concat()方法:數組合并。
const oArray
= avengers
. concat ( heroes
) ;
const oArray
= [ ... avengers
, ... heroes
] ;
join()方法:數組變成組合了數組所有元素的字符串。
const a
= avengers
. join ( `&` ) ;
slice()方法:從原始數組中切掉一片,從而創建一個子數組。
const b
= avengers
. slice ( 2 , 3 ) ;
splice()方法:從一個數組中刪除元素,然后將新元素插入在被刪除的元素的位置上。
const cc
= avengers
. splice ( 2 , 2 , 'liwanling' , 'li' ) ;
reverse()方法:反轉數組中元素的次序(永久性改變)。
const e
= d
. reverse ( ) ;
sort()方法:對數組中的元素按字母順序進行排序(永久性改變)。
const g
= f
. sort ( ) ;
indexOf():檢測數組中是否包含一個特定值,如果找到了,就返回該值在數組中第一次出現的索引號,否則,就返回-1。
const avengers
= [ '美國隊長' , '鋼鐵俠' , '雷神' , '綠巨人' ] ;
const a1
= avengers
. indexOf ( '美國隊長1' ) ;
console
. log ( a1
) ;
includes():檢測數組中是否包含特定值,如果找到了,就返回true,否則就返回false。
const a2
= avengers
. includes ( '美國隊長2' ) ;
Set:沒有重復值
1.創建Set
構造器
let oSet
= new Set ( ) ;
2.添加元素
oSet
. add ( 1 ) . add ( 2 )
3.Set的屬性和方法
size屬性:獲取集合中值的數目。
console
. log ( list4
. size
) ;
has()方法:用于檢測一個值是否在集合中,該方法會返回true或者false。
console
. log ( list4
. has ( 'brown' ) ) ;
delete()方法:從集合中刪除一個值。
list4
. delete ( 'the' ) ;
clear():刪掉集合中的所有值。
list4
. clear ( ) ;
4.Set和數組的轉換:擴展運算符 Array.from()方法
const oArray
= [ ... list4
] ; const oArray
= Array
. from ( list4
) ;
let oSet
= new Set ( [ 1 , 2 , 3 ] ) ;
5.WeakSet
const weak
= new WeakSet ( ) . add ( array1
) . add ( array2
) ;
console
. log ( weak
) ;
array1
= null ;
array2
= null ;
const array3
= [ ... weak
] [ 0 ] ;
const array4
= [ ... weak
] [ 1 ] ;
Map
1.創建Map
const romanNumerals
= new Map ( ) ;
2.方法和屬性
size屬性:獲取鍵和值的數量。
console
. log ( romanNumerals
. size
) ;
get(key):通過鍵獲取值。
console
. log ( romanNumerals
. get ( 3 ) ) ;
has(key):檢測一個特定鍵是否在映射中。
console
. log ( romanNumerals
. has ( 5 ) ) ;
delete(key):從映射中刪除一個鍵值對。
romanNumerals
. delete ( 5 ) ;
clear():從映射中刪除所有鍵值對。
console
. log ( romanNumerals
) ;
3.Map轉換為數組:擴展運算符 Array.from() 方法
const oArray1
= [ ... romanNumerals
] ;
const oArray2
= Array
. from ( romanNumerals
) ;
4.WeakMap
strong1
= new Map ( ) . set ( array5
) . set ( array6
) ;
weak1
= new WeakMap ( ) . set ( array9
) . set ( array10
) ;
總結
以上是生活随笔 為你收集整理的1_01李婉玲_数据结构_1012 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。