日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

1_01李婉玲_数据结构_1012

發布時間:2023/12/15 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 1_01李婉玲_数据结构_1012 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Day03作業 數據結構

概述 基本類型和引用類型

基本類型:String Boolean Number Null Undefined Symbol Bigint。

引用類型:Object(存儲屬性和屬性值) Function。

數組(Array):有序的值的列表。
集合(Set):唯一值的集合。
特殊集合(WeakSet):特殊Set。
映射(Map):鍵值對。
特殊映射(WeakMap):特殊Map。

區別:

  • (內存存儲)基本類型存儲在棧(Stack)里面;引用類型在棧上存儲對堆上數據的引用(指針),在堆(Heap)上存儲引用類型本身的數據。
  • (清理方式)基本類型函數和方法執行完就清理內存;引用類型采用垃圾回收機制。
  • 對象 存儲屬性和屬性值

    定義對象:

    • Object構造器
    // 方式一:盡量不使用這種方法 // 聲明對象 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); //Object

    數組 有序的值的列表

    (與其他編程語言相比)區別:

  • 數組的每個槽位可以存儲任意類型的數據。
  • 動態大小 —— 影響性能。
  • 定義數組:(聲明)

    • 數組構造器
    const oArray = new Array();
    • 數組字面量
    const oArray = []; console.log(typeof oArray); // Object

    初始化:

    // 數組初始化 const heroes = [];// 未賦值 console.log(heroes[0]);// Undefinedconst heroes = []; heroes[0] = '蝙蝠俠'; heroes[1] = '神奇女俠'; heroes[2] = '閃電俠'; heroes[5] = '水行俠';// 表示六個元素,其他元素為Undefined 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];// 兩個變量a,b console.log(a);

    數組的屬性和方法:

  • length屬性:數組的長度。
  • const avengers = ['美國隊長','鋼鐵俠','雷神','綠巨人']; console.log(avengers.length);// 通過設置length屬性改變數組元素(不可逆轉的改變,無副本) avengers.length = 1; console.log(avengers);// [ '美國隊長' ] avengers.length = 5; console.log(avengers);// <1 empty item> Undefined// 不改變數組元素:將length設為只讀屬性get。
  • pop()方法:刪掉數組中最后一個元素。
  • // 聲明數組 const avengers = ['美國隊長','鋼鐵俠','雷神','綠巨人']; // 數組維數縮小 console.log(avengers.pop()); console.log(avengers);// delete運算符:元素個數沒變,刪掉元素變為Undefined 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);// 新建數組,進行合并。// 擴展運算符:ES6新增。 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');// [ '美國隊長', '鋼鐵俠', 'liwanling', 'li', 'wang' ] console.log(c);// [ [ 'liwanling', 'li' ] ] console.log(cc);// 返回切掉的兩個元素 [ '雷神', '綠巨人' ] console.log(ccc);// console.log(avengers);
  • reverse()方法:反轉數組中元素的次序(永久性改變)。
  • const d = ['a','b','c','d']; const e = d.reverse();// 賦值給變量 console.log(e,d);// [ 'd', 'c', 'b', 'a' ] [ 'd', 'c', 'b', 'a' ]
  • sort()方法:對數組中的元素按字母順序進行排序(永久性改變)。
  • const f = [1,2,10,1,2];// ASCII排列 const g = f.sort(); console.log(g,f);// [ 1, 1, 10, 2, 2 ] [ 1, 1, 10, 2, 2 ]
  • indexOf():檢測數組中是否包含一個特定值,如果找到了,就返回該值在數組中第一次出現的索引號,否則,就返回-1。
  • const avengers = ['美國隊長','鋼鐵俠','雷神','綠巨人']; const a1 = avengers.indexOf('美國隊長1'); console.log(a1);// 0
  • includes():檢測數組中是否包含特定值,如果找到了,就返回true,否則就返回false。
  • const avengers = ['美國隊長','鋼鐵俠','雷神','綠巨人']; const a2 = avengers.includes('美國隊長2'); console.log(a2);// false

    多維數組:

  • 作用:坐標系統(二維數組)以及復雜算法。

  • 舉例:

  • 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);// [ [ 'Jun', 'Jul', 'Aug' ], [ 'Dec', 'Jan', 'Feb' ] ] // 擴展運算符扁平化為字符串 const summer = ['Jun','Jul','Aug']; const winter = ['Dec','Jan','Feb']; const flat = [...summer,...winter]; console.log(flat);// [ 'Jun', 'Jul', 'Aug', 'Dec', 'Jan', 'Feb' ]

    數組去重:(Set和數組的轉換)

    const a = [1,2,12,1,2,3,4,5];// 循環讀取一個一個判斷 const b = new Set(a); console.log(b);// Set { 1, 2, 12, 3, 4, 5 } 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)。
    // Set強集合 內存泄漏 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]; // 1 array4 = [...strong][1]; console.log(array3); console.log(array4);// 數組還在// WeakSet 避免內存泄漏 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);// Map { 1 => 'I', 2 => 'II', 3 => 'III', 4 => 'IV', 5 => 'V' }

    方法和屬性:

  • 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);// 5
  • 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));// III
  • 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));// true
  • 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);// Map { 1 => 'I', 2 => 'II', 3 => 'III', 4 => 'IV' }
  • 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轉換為數組:

    • 擴展運算符
    • Array.from() 方法
    //map to array 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); // [ [ 1, 'I' ], [ 2, 'II' ], [ 3, 'III' ], [ 4, 'IV' ], [ 5, 'V' ] ] // [ [ 1, 'I' ], [ 2, 'II' ], [ 3, 'III' ], [ 4, 'IV' ], [ 5, 'V' ] ]

    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);// [ [ 1, 'I' ], undefined ] console.log(array8);// [ [ 2, 'II' ], undefined ]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);// 0

    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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。