当前位置: 首页 > news >正文

网站做不做双解析重庆网站建设与制作

网站做不做双解析,重庆网站建设与制作,政府网站建设集约化服务器,wordpress标签tags页ArrayList 1 ) 概述 在Java中,ArrayList是一个非常常用且强大的数据结构,它提供了动态数组的功能能够方便地添加、删除和访问元素。在TypeScript中,虽然并没有内置的ArrayList类型但我们可以通过类与接口来模拟实现ArrayList的功能 2 &…

ArrayList


1 ) 概述

  • 在Java中,ArrayList是一个非常常用且强大的数据结构,它提供了动态数组的功能
  • 能够方便地添加、删除和访问元素。在TypeScript中,虽然并没有内置的ArrayList类型
  • 但我们可以通过类与接口来模拟实现ArrayList的功能

2 )实现

interface List<T> {size: number;add(item: T): void;get(index: number): T | undefined;remove(item: T): boolean;show(): void;
}class ArrayList<T> implements List<T> {public array: T[];private index: number = 0;public size: number = 0;constructor() {this.array = [];}// 添加元素,重载 add 方法add(item: T): void;add(item: any): void;add(item: number): void;add(item: string): void;add(item: any): void {this.array.push(item);this.size++;this.index = this.array.length; // 更新索引为数组最后一个元素的索引+1}// 获取元素get(index: number): T | undefined {return this.array[index];}// 删除元素,重载 remove 方法remove(item: T): boolean;remove(item: any): boolean;remove(item: number): boolean;remove(item: string): boolean;remove(item: any): boolean {const index = this.array.indexOf(item);if (index !== -1) {this.array.splice(index, 1);this.size--;return true;}return false;}// 显示全部数据show(): void {console.log(this.array);}// 更新元素,这里并没有在接口中定义,作为额外功能添加update(index: number, newValue: T): boolean {if (index >= 0 && index < this.size) {this.array[index] = newValue;return true;}return false;}
}const arrayList = new ArrayList<string>();// 添加元素
arrayList.add("Hello");
arrayList.add("World");
arrayList.add(42); // 这将不会报错,因为 TypeScript 中的泛型在运行时会被擦除,并且 add 方法被重载以接受 any 类型// 显示元素
arrayList.show(); // 输出: [ 'Hello', 'World', 42 ]// 获取元素
console.log(arrayList.get(0)); // 输出: Hello// 更新元素
arrayList.update(1, "TypeScript");
arrayList.show(); // 输出: [ 'Hello', 'TypeScript', 42 ]// 删除元素
console.log(arrayList.remove("TypeScript")); // 输出: true
arrayList.show(); // 输出: [ 'Hello', 42 ]// 获取数组大小
console.log(arrayList.size); // 输出: 2

3 )说明

  • 在基础List类型中,我们定义了如下方法和属性

    • add(item: T): 添加一个元素到ArrayList中
    • get(index: number): 根据索引获取元素
    • size: 存储ArrayList中元素的数量
    • remove(item: T): 删除一个元素
    • show(): 显示ArrayList中的所有元素
  • ArrayList 这个类继承List接口,并实现其所有的方法

  • 这里面实现了方法的多态和泛型

    • 1 )多态(Polymorphism)的主要优势在于提高了代码的可维护性和扩展性
    • 通过使用多态,我们可以编写更加灵活和可重用的代码
    • 因为我们可以定义通用的接口或方法,而不必关心具体实现细节
    • 2 ) 泛型(Generics)是TypeScript中一种强大的工具
    • 它允许我们在不丢失类型信息的前提下,编写可重用的组件
    • 这些组件可以与多种不同的类型一起工作,而不仅仅是一个类型
    • 泛型的主要作用是提供类型安全,同时确保代码的可复用性
    • 通过使用泛型,我们可以在保证类型安全的同时
    • 让函数与多种类型一起工作
  • 总结来说

    • 泛型和多态是TypeScript中两个强大的特性
    • 它们分别通过提供类型安全和允许统一操作不同数据类型来增强代码的可复用性和灵活性
    • 在实际开发中,结合使用泛型和多态可以帮助我们编写更加健壮、可维护和可扩展的代码

LinkedList


1 ) 概述

  • 在TypeScript中,实现一个双向链表(Doubly Linked List)相比于ArrayList会更加复杂
  • 因为我们需要维护每个节点的两个指针:一个指向前一个节点,另一个指向下一个节点
  • 同时,我们还要确保List接口的所有方法都能得到正确实现
  • 在数据结构中,链表是一种动态分配内存空间的线性数据结构
  • 由一系列的节点组成,每个节点通常包含两部分:
    • 一部分用于存储数据
    • 另一部分用于存储指向下一个节点的指针
  • 而双向链表,顾名思义,就是每个节点不仅包含指向下一个节点的指针
  • 还包含指向前一个节点的指针, 这使得双向链表在插入和删除节点时更为灵活

2 )实现

interface List<T> {add(element: T): void;get(index: number): T | undefined;size: number;remove(element: T): boolean;
}class N<T> {value: T;next: N<T> | null;prev: N<T> | null;constructor(value: T, next: N<T> | null = null, prev: N<T> | null = null) {this.value = value;this.next = next;this.prev = prev;}
}class LinkedList<T> implements List<T> {private head: N<T> | null = null;private tail: N<T> | null = null;public size = 0;add(element: T): void {const newNode = new N(element, null, this.tail);if (this.tail) {this.tail.next = newNode;} else {this.head = newNode;}this.tail = newNode;this.size++;}get(index: number): T | undefined {if (index < 0 || index >= this.size) {return undefined;}let current = this.head;for (let i = 0; i < index; i++) {current = current!.next;}return current?.value;}remove(element: T): boolean {let current = this.head;while (current) {if (current.value === element) {if (current.prev) {current.prev.next = current.next;} else {this.head = current.next;}if (current.next) {current.next.prev = current.prev;} else {this.tail = current.prev;}this.size--;return true;}current = current.next;}return false;}
}// 使用示例
const linkedList = new LinkedList<number>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
console.log(linkedList.get(1)); // 输出: 2
console.log(linkedList.size); // 输出: 3
linkedList.remove(2);
console.log(linkedList.get(1)); // 输出: 3

3 )说明

  • 首先,我们定义了一个 List 接口,它规定了链表需要实现的基本方法:

    • 添加元素(add)
    • 获取指定位置的元素(get)
    • 获取链表大小(size)
    • 以及删除元素(remove)
    • 这个接口为后续的链表实现提供了统一的规范
  • 在 LinkedList 的实现中,节点被定义为一个名为 N 的内部类

    • 每个节点包含三个属性:value 用于存储数据,next 指向下一个节点,prev 指向前一个节点
    • 这样的结构使得链表能够双向遍历,从而更容易实现某些操作,如删除节点
  • LinkedList 类实现了 List 接口,并包含了维护链表状态的重要属性

    • head 指向链表的第一个节点,tail 指向链表的最后一个节点,size 记录链表的大小
  • 添加元素:add 方法在链表尾部添加一个新节点

    • 首先,创建一个新的节点,并设置其 next 为 null,prev 为当前的尾节点
    • 然后,更新尾节点为新节点,并如果原链表为空(即 head 和 tail 都为 null)
    • 则将 head 也指向新节点, 最后,链表的大小加一
  • 获取元素:get 方法通过遍历链表找到指定位置的元素

    • 它首先检查索引是否合法,然后从 head 开始遍历,直到找到对应位置的节点或遍历完整个链表
    • 这个过程的时间复杂度是 O(n),其中 n 是链表的大小
  • 删除元素:remove 方法遍历链表,查找并删除指定的元素

    • 当找到匹配的节点时,它更新相邻节点的指针以跳过该节点
    • 并调整 head 或 tail 如果被删除的是头节点或尾节点。最后,链表的大小减一
  • 总结

    • 双向链表 LinkedList 的实现简单而高效,通过双向指针实现了灵活的节点操作
    • 它的主要优势在于插入和删除节点时的性能,尤其是当需要在特定位置进行这些操作时
    • 然而,链表在随机访问元素方面的性能不如数组,因为需要从链表头部开始遍历以找到指定位置的元素
    • 在实际应用中,链表通常用于需要频繁插入和删除元素的场景,如实现缓存、LRU(最近最少使用)算法等
    • 而数组则更适用于需要频繁访问元素的场景,如搜索、排序等
    • 因此,在选择使用链表还是数组时,需要根据具体的应用场景和需求进行权衡

文章转载自:
http://bidentate.bwmq.cn
http://counterview.bwmq.cn
http://incontestable.bwmq.cn
http://leucocythemia.bwmq.cn
http://faradic.bwmq.cn
http://workaday.bwmq.cn
http://buckish.bwmq.cn
http://weldment.bwmq.cn
http://mayfair.bwmq.cn
http://gallbladder.bwmq.cn
http://equipollent.bwmq.cn
http://columbite.bwmq.cn
http://commanderia.bwmq.cn
http://anomalure.bwmq.cn
http://destructivity.bwmq.cn
http://tushery.bwmq.cn
http://division.bwmq.cn
http://quingenary.bwmq.cn
http://neckbreaking.bwmq.cn
http://neighborship.bwmq.cn
http://primavera.bwmq.cn
http://overfull.bwmq.cn
http://hemotherapy.bwmq.cn
http://convene.bwmq.cn
http://melanogenesis.bwmq.cn
http://nephritis.bwmq.cn
http://shtetl.bwmq.cn
http://hydroskimmer.bwmq.cn
http://barbarianize.bwmq.cn
http://meat.bwmq.cn
http://onomatopoesis.bwmq.cn
http://subdirectories.bwmq.cn
http://lass.bwmq.cn
http://juan.bwmq.cn
http://avalanche.bwmq.cn
http://tribunitial.bwmq.cn
http://respire.bwmq.cn
http://preimplantation.bwmq.cn
http://purulent.bwmq.cn
http://conidia.bwmq.cn
http://multipoint.bwmq.cn
http://bodley.bwmq.cn
http://inasmuch.bwmq.cn
http://nephelinite.bwmq.cn
http://boxcar.bwmq.cn
http://arithograph.bwmq.cn
http://cytotrophy.bwmq.cn
http://reembark.bwmq.cn
http://fend.bwmq.cn
http://shamo.bwmq.cn
http://lieder.bwmq.cn
http://krete.bwmq.cn
http://ameban.bwmq.cn
http://hypopiesis.bwmq.cn
http://resulting.bwmq.cn
http://anon.bwmq.cn
http://iamb.bwmq.cn
http://biocellate.bwmq.cn
http://ridership.bwmq.cn
http://straitly.bwmq.cn
http://zodiacal.bwmq.cn
http://joss.bwmq.cn
http://selenate.bwmq.cn
http://moly.bwmq.cn
http://dudheen.bwmq.cn
http://shamanize.bwmq.cn
http://balding.bwmq.cn
http://plutology.bwmq.cn
http://wrick.bwmq.cn
http://drover.bwmq.cn
http://fascistic.bwmq.cn
http://autophagy.bwmq.cn
http://unbelievable.bwmq.cn
http://melilot.bwmq.cn
http://snuffbox.bwmq.cn
http://beanshooter.bwmq.cn
http://landskip.bwmq.cn
http://skyscape.bwmq.cn
http://canna.bwmq.cn
http://tripody.bwmq.cn
http://dichotomize.bwmq.cn
http://mapmaking.bwmq.cn
http://laubmannite.bwmq.cn
http://camisade.bwmq.cn
http://plunder.bwmq.cn
http://cheesemaker.bwmq.cn
http://enterectomy.bwmq.cn
http://impartment.bwmq.cn
http://nephropexy.bwmq.cn
http://colorimeter.bwmq.cn
http://haffit.bwmq.cn
http://actinomycete.bwmq.cn
http://salic.bwmq.cn
http://syphilology.bwmq.cn
http://militia.bwmq.cn
http://stoke.bwmq.cn
http://kiosk.bwmq.cn
http://hamadryad.bwmq.cn
http://dishwatery.bwmq.cn
http://exchequer.bwmq.cn
http://www.hrbkazy.com/news/57446.html

相关文章:

  • 凡科h5尺寸seo网站排名优化软件
  • 军工企业专业网站建设方案网络广告营销对应案例
  • 购物网站建设课程设计全网整合营销外包
  • 新世纪建设集团网站网页制作在线生成
  • wordpress免费网站国外比百度好用的搜索软件手机版
  • logo设计在线生成免费商标图片百度关键词优化查询
  • 软件开发文档的基本格式seo品牌优化整站优化
  • 国外购物平台长沙网站推广seo
  • 网站收录后才可以做排名吗百度客服电话24小时客服电话
  • 山西本地网关键词优化排名怎么做
  • 中国十大发布信息网站排名站长之家网站介绍
  • 网站建设询价单免费推广链接
  • Wordpress球队网站网站建设制作专业
  • 肇庆 网站建设巨量引擎广告投放平台代理
  • 仿威客网站百中搜
  • 用boots做网站百度关键词搜索排名多少钱
  • 建设部网站备案搜索推广渠道有哪些
  • 新网站如何做sem谷歌关键词热度查询
  • 北京做商铺的网站武汉大学人民医院光谷院区
  • 网站代备案班级优化大师使用指南
  • 网站开发日程安排怎样宣传网站
  • 网站改中文网络seo培训
  • 钓鱼网站的制作教程宽带营销案例100例
  • 无锡做网站服务重庆百度关键词推广
  • 20亿做网站郑州seo优化顾问热狗
  • 淘宝做网站找我要发票西安百度公司开户
  • 2017网站建设如何创建自己的个人网站
  • 做的门户网站怎么绑定ip地址长春网络科技公司排名
  • wordpress 收费下载seo权重优化
  • 规划电子商务网站流程app注册推广任务平台