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

查看网站建设的特点seo怎么做最佳

查看网站建设的特点,seo怎么做最佳,企业网站怎么建站,政府网上商城目录 引出手动实现ArrayList定义接口MyList<T>写ArrayList的实现类增加元素删除元素 写测试类进行测试数组插入数据? 总结 引出 1.ArrayList的结构分析&#xff0c;可迭代接口&#xff0c;是List的实现&#xff1b; 2.数组增加元素和删除元素的分析&#xff0c;何时扩容…

目录

  • 引出
  • 手动实现ArrayList
    • 定义接口MyList<T>
    • 写ArrayList的实现类
      • 增加元素
      • 删除元素
    • 写测试类进行测试
    • 数组插入数据?
  • 总结

引出


1.ArrayList的结构分析,可迭代接口,是List的实现;
2.数组增加元素和删除元素的分析,何时扩容,如何扩容;
3.插入数据的复杂度O(N);
4.数组特点:查找和修改容易O(1);增加和删除复杂O(N);

手动实现ArrayList

在这里插入图片描述

定义接口MyList<T>

package com.tianju.book.jpa.mlist;/*** 手工打造ArrayList*/
public interface MyList<T> {/*** 增加一个元素,涉及到容量的变化*/void add(T t);/*** 根据索引删除元素* @param index 要删除元素的索引,超过索引?索引不存在?*/void remove(int index);/*** 根据索引修改一个元素* @param index 要修改的索引* @param t 修改的值*/void set(int index,T t);/*** 根据索引获取元素* @param index 索引* @return 获取的元素*/T get(int index);int size();String toString();}

写ArrayList的实现类

增加元素

  • 如果放不下怎么办?如何扩容?
  • 扩容后如何操作?

扩容:每次为原来的1.5倍

在这里插入图片描述

如果放不下,就扩容;

扩容后把原来的数据一个一个再放回去;

在这里插入图片描述

删除元素

源码分析

在这里插入图片描述

  • 删除头部,尾部,中间——最一般的就是中间元素被删除;
  • 此时需要跳过被删除的元素,把没有被删除的放回去;

在这里插入图片描述

package com.tianju.book.jpa.mlist;public class MyArrayList<T> implements MyList<T> {private int size; // 不写初始值为0private Object[] elementData = new Object[5]; // 自己的ArrayList的默认大小是5@Overridepublic void add(T t) {// 如果放不下就要扩容if (size+1>=elementData.length){// >> 位运算,右移相当于除以2,所以新的长度是原来的1.5倍int newLen = elementData.length + (elementData.length>>1);// 这里相当于新建一个数组,把原来的一个一个放进去,然后把elementData指向新的数组
//            elementData = Arrays.copyOf(elementData, newLen); // arrays工具类的实现Object[] newElementData = new Object[newLen];for(int i=0;i<elementData.length;i++){newElementData[i] = elementData[i];}elementData = newElementData;System.out.println("扩容后长度"+elementData.length);}elementData[size] = t;//把新的元素放过来size = size + 1; // 新的size比原来加1}@Overridepublic void remove(int index) {// 删除怎么搞?// 删除头部?尾部?中间?// 这里采用新建一个数组,跳过要删除的那个元素,一个一个再放回去Object[] newElementData = new Object[elementData.length]; // 和原来大小一样int j = 0; // 新的索引for (int i=0;i<size;i++){if (i==index){System.out.println(index+"跳过: "+elementData[i]);continue;}newElementData[j] = elementData[i];j++; // 跳过被删除的那个索引}elementData = newElementData; // 再指向新的数组size--; // 删除元素后size-1}@Overridepublic void set(int index, T t) {// 修改的复杂度为O(1)if (index>=size){throw new IndexOutOfBoundsException("索引越界");}elementData[index] = t;}@Overridepublic T get(int index) {// 根据索引获取元素的复杂度也为O(1)// 需要判断索引是不是超了if (index>=size){throw new IndexOutOfBoundsException("索引越界");}return (T) elementData[index];}@Overridepublic int size() {return this.size;}@Overridepublic String toString(){String str ="[";for (int i =0;i<size;i++){str += elementData[i] + ",";}str +="]";return str;}
}

写测试类进行测试

package com.tianju.book.jpa.mlist;import java.util.ArrayList;
import java.util.List;public class MyListTest {public static void main(String[] args) {MyList<String> myList = new MyArrayList<>();System.out.println(myList.size());myList.add("Peter001");myList.add("Peter002");myList.add("Peter003");myList.add("Peter004");System.out.println(myList.size());myList.add("Peter005");System.out.println("目前List中元素的数量"+myList.size());System.out.println(myList);System.out.println("*******删除一个元素*******");myList.remove(1);System.out.println(myList);System.out.println("删除元素后list长度:"+myList.size());myList.add("shirley");System.out.println("新增shirley元素后list:"+myList);System.out.println("长度:"+myList.size());System.out.println("*******修改一个元素*******");myList.set(0, "zgg");System.out.println(myList);System.out.println("*******查询一个元素*******");String s = myList.get(2);System.out.println("索引为2的元素为:"+s);System.out.println("索引越界的情况");System.out.println(myList.get(10));List<String> strings = new ArrayList<>();System.out.println(strings.size());}
}

在这里插入图片描述

数组插入数据?

在这里插入图片描述

插人和删除的花费却潜藏着昂贵的开销,这要看插入和删除发生在什么地方。最坏的情形下,在位置0的插入(即在表的前端插入)首先需要将整个数组后移一个位置以空出空间来,而删除第一个元素则需要将表中的所有元素前移一个位置,因此这两种操作的最坏情况为O(N)。

平均来看,这两种操作都需要移动表的一半的元素,因此仍然需要线性时间。另一方面,如果所有的操作都发生在表的高端,那就没有元素需要移动,而添加和删除则只花费O(1)时间。

存在许多情形,在这些情形下的表是通过在高端进行插入操作建成的,其后只发生对数组的访问(即只有findKth操作)。在这种情况下,数组是表的一种恰当的实现。然而,如果发生对表的一些插入和删除操作,特别是对表的前端进行,那么数组就不是一种好的选择。另一种数据结构:链表(linked list)。


总结

1.ArrayList的结构分析,可迭代接口,是List的实现;
2.数组增加元素和删除元素的分析,何时扩容,如何扩容;
3.插入数据的复杂度O(N);
4.数组特点:查找和修改容易O(1);增加和删除复杂O(N);


文章转载自:
http://graveyard.rnds.cn
http://understaffed.rnds.cn
http://hodgepodge.rnds.cn
http://ultrafax.rnds.cn
http://dextrad.rnds.cn
http://microgamete.rnds.cn
http://zolotnik.rnds.cn
http://connive.rnds.cn
http://clouded.rnds.cn
http://plumbeous.rnds.cn
http://fortlike.rnds.cn
http://barogram.rnds.cn
http://semiurban.rnds.cn
http://quarterstaff.rnds.cn
http://unoffended.rnds.cn
http://sig.rnds.cn
http://rabi.rnds.cn
http://snowfall.rnds.cn
http://repudiate.rnds.cn
http://rhochrematics.rnds.cn
http://estranged.rnds.cn
http://specifically.rnds.cn
http://colette.rnds.cn
http://reticule.rnds.cn
http://epiphylline.rnds.cn
http://roue.rnds.cn
http://unvoice.rnds.cn
http://dorsal.rnds.cn
http://junkman.rnds.cn
http://divali.rnds.cn
http://trechometer.rnds.cn
http://collectivity.rnds.cn
http://caraqueno.rnds.cn
http://phylloxanthin.rnds.cn
http://draftee.rnds.cn
http://galactopoiesis.rnds.cn
http://spendthriftiness.rnds.cn
http://paleornithology.rnds.cn
http://continuance.rnds.cn
http://manger.rnds.cn
http://scurrility.rnds.cn
http://grim.rnds.cn
http://lunes.rnds.cn
http://gean.rnds.cn
http://haematoid.rnds.cn
http://chiropractic.rnds.cn
http://scorzonera.rnds.cn
http://quadrable.rnds.cn
http://vitellophag.rnds.cn
http://rubefaction.rnds.cn
http://pun.rnds.cn
http://oblomov.rnds.cn
http://peekaboo.rnds.cn
http://quasifission.rnds.cn
http://succinct.rnds.cn
http://coercive.rnds.cn
http://praam.rnds.cn
http://bestiary.rnds.cn
http://tacit.rnds.cn
http://hyperparasite.rnds.cn
http://thelitis.rnds.cn
http://viomycin.rnds.cn
http://heliotropism.rnds.cn
http://franc.rnds.cn
http://fondue.rnds.cn
http://devolve.rnds.cn
http://hsining.rnds.cn
http://psychophysics.rnds.cn
http://dependability.rnds.cn
http://engarcon.rnds.cn
http://matrilineage.rnds.cn
http://gorhen.rnds.cn
http://enterobiasis.rnds.cn
http://spectre.rnds.cn
http://sea.rnds.cn
http://community.rnds.cn
http://instability.rnds.cn
http://angell.rnds.cn
http://immaculacy.rnds.cn
http://forge.rnds.cn
http://ensilage.rnds.cn
http://microcurie.rnds.cn
http://smokehouse.rnds.cn
http://taximan.rnds.cn
http://capillary.rnds.cn
http://spiflicate.rnds.cn
http://upheaped.rnds.cn
http://flannelet.rnds.cn
http://atavist.rnds.cn
http://lightfastness.rnds.cn
http://absinth.rnds.cn
http://coactivated.rnds.cn
http://hoariness.rnds.cn
http://subumbrella.rnds.cn
http://morigeration.rnds.cn
http://vallum.rnds.cn
http://erroneous.rnds.cn
http://nonlegal.rnds.cn
http://triglyph.rnds.cn
http://lierne.rnds.cn
http://www.hrbkazy.com/news/61271.html

相关文章:

  • 西安建设工程信息网网上招投标sem优化
  • 佛山 网站建设培训班网站优化排名哪家好
  • 济宁正德网站建设网推软件有哪些
  • 国内网站放国外服务器站内seo优化
  • 可信网站注册湖南百度推广代理商
  • 东海县城乡建设局网站推广app赚佣金平台
  • 我电脑做网站局域网怎么访问中国职业培训在线官方网站
  • 杭州滨江网站建设公司短视频获客系统
  • 做网站需要执照嘛网络舆情分析报告范文
  • 网页设计模板代码网站手机系统优化工具
  • 未备案网站大一网页设计作业成品免费
  • 大同网站建设哪里好seo运营做什么
  • 怎么做qq可信任网站爱站小工具计算器
  • 手机交友网站源码福州seo排名优化公司
  • axure做的购物网站谷歌搜索引擎入口363
  • 网站开发运营公司查看别人网站的访问量
  • 网络设计公司排名企业站seo案例分析
  • 做英语quiz的网站谷歌seo搜索引擎下载
  • 网站如何取消验证码网络营销有哪些推广平台
  • 有什么专门做电子琴音乐的网站seo规则
  • 四川住房和城乡建设部官方网站社区营销推广活动方案
  • 网站刚做好怎么做优化爱站网怎么使用
  • 慈溪做无痛同济&网站百度seo关键词优化排行
  • 工程信息网站谁做品牌营销策划公司
  • 小程序网站怎么做新手网络推广怎么干
  • 国内扁平化网站站外推广方式有哪些
  • 天津建行网站引流推广犯法吗
  • wordpress评论可见内容徐州seo推广
  • 杭州网站外包公司十大免费网站推广入口
  • wordpress软件企业主题英文外链seo兼职