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

深圳定制网站制作费用营销网络

深圳定制网站制作费用,营销网络,做封面下载网站,免费网站正能量不用下载⭐ 作者:小胡_不糊涂 🌱 作者主页:小胡_不糊涂的个人主页 📀 收录专栏:浅谈Java 💖 持续更文,关注博主少走弯路,谢谢大家支持 💖 线性表与顺序表 1. 线性表2. 顺序表2.1 …

⭐ 作者:小胡_不糊涂
🌱 作者主页:小胡_不糊涂的个人主页
📀 收录专栏:浅谈Java
💖 持续更文,关注博主少走弯路,谢谢大家支持 💖

线性表与顺序表

  • 1. 线性表
  • 2. 顺序表
    • 2.1 接口的实现
  • 3. ArrayList简介
  • 4. ArrayList使用
    • 4.1 ArrayList构造
    • 4.2 ArrayList常见操作
    • 4.3 ArrayList的遍历

在这里插入图片描述

1. 线性表

线性表(linear list) 是n个具有相同特性的数据元素的有限序列。 它是一种在实际中广泛使用的数据结构,常见的线性表:顺序表、链表、栈、队列…

线性表在逻辑上是线性结构,也就说是连续的一条直线。但是在物理结构上并不一定是连续的,线性表在物理上存储时,通常以数组和链式结构的形式存储。

在这里插入图片描述

2. 顺序表

顺序表 是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。

2.1 接口的实现

实现顺序表的插入、删除、修改、查找工作:

import java.util.Arrays;
public class MyArrayList {public int[] elem ;//定义一个数组public int usedSize;//0,用来记录元素个数//顺序表的 默认大小public static final int DEFAULT_SIZE = 10;public MyArrayList() {this.elem = new int[DEFAULT_SIZE];}public MyArrayList(int capacity) {this.elem = new int[capacity];}//顺序表尾部插入元素public void add(int data) {checkCapacity();this.elem[this.usedSize] = data;this.usedSize++;}//判断顺序表是否为空public boolean isFull() {/*if(usedSize == elem.length) {return true;}return false;*/return usedSize == elem.length;}//指定pos位置添加元素datapublic void add(int pos, int data) {try {checkPosOnAdd(pos);}catch (PosIllegality e) {e.printStackTrace();}checkCapacity();//1、从最后一个有效的数据开始往后移动 //2、当i < pos 就结束for (int i = usedSize-1; i >= pos; i--) {elem[i+1] = elem[i];}//3、存放元素到pos 位置elem[pos] = data;//4、usedSize++;usedSize++;}//检查插入位置pos的合法性private void checkPosOnAdd(int pos) throws PosIllegality{if(pos < 0 || pos > usedSize) {System.out.println("不符合法!");throw new PosIllegality("插入元素下标异常: "+pos);}}private void checkCapacity() {if(isFull()) {//扩容elem = Arrays.copyOf(elem,elem.length*2);}}//判定是否包含某一元素public boolean contains(int toFind) {if(isEmpty()) {return false;}for (int i = 0; i < usedSize; i++) {//如果是查找引用数据类型 一定记住 重写方法if(elem[i] == toFind) {return true;}}return false;}//判断线性表是否为空public boolean isEmpty() {return usedSize == 0;}//查找某个元素对应位置public int indexOf(int toFind) {if(isEmpty()) {return -1;}for (int i = 0; i < usedSize; i++) {//如果是查找引用数据类型 一定记住 重写方法if(elem[i] == toFind) {return i;}}return -1;}//获取pos位置元素public int get(int pos) throws MyArrayListEmpty{checkPosOnGetAndSet(pos);if(isEmpty()) {throw new MyArrayListEmpty("获取指定下标元素时" +"顺序表为空!");}return elem[pos];}private void checkPosOnGetAndSet(int pos) throws PosIllegality{if(pos < 0 || pos >= usedSize) {System.out.println("不符合法!");throw new PosIllegality("获取指定下标的元素异常: "+pos);}}//给pos位置设为元素valuepublic void set(int pos, int value) {checkPosOnGetAndSet(pos);elem[pos] = value;}//删除第一次出现的这个数字public void remove(int toRemove) {int index = indexOf(toRemove);//获取元素下标if(index == -1) {System.out.println("没有这个数字!");return;}for(int i = index; i < usedSize-1;i++) {elem[i] = elem[i+1];}usedSize--;}//获取顺序表长度public int size() {return this.usedSize;}//清空顺序表public void clear() {this.usedSize = 0;}//打印顺序表中元素public void display() {for (int i = 0; i < this.usedSize; i++) {System.out.print(this.elem[i]+" ");}System.out.println();}}

测试类:

public class TestMAL{public static void main(String[] arg) {MyArrayList arr = new MyArrayList();arr.add(1);arr.add(2);arr.add(3);arr.add(4);arr.add(5);arr.display();System.out.println("-------");arr.add(1);arr.display();System.out.println("-------");arr.add(6,2);arr.display();System.out.println("-------");System.out.println(arr.indexOf(3));arr.display();System.out.println("-------");arr.get(9);}
}

3. ArrayList简介

在集合框架中,ArrayList是一个普通的类,实现了List接口,具体框架图如下:

在这里插入图片描述

【说明】

  1. ArrayList是以泛型方式实现的,使用时必须要先实例化
  2. ArrayList实现了RandomAccess接口,表明ArrayList支持随机访问
  3. ArrayList实现了Cloneable接口,表明ArrayList是可以clone的
  4. ArrayList实现了Serializable接口,表明ArrayList是支持序列化的
  5. 和Vector不同,ArrayList不是线程安全的,在单线程下可以使用,在多线程中可以选择Vector或者CopyOnWriteArrayList
  6. ArrayList底层是一段连续的空间,并且可以动态扩容,是一个动态类型的顺序表

4. ArrayList使用

4.1 ArrayList构造

方法解释
ArrayList()无参构造
ArrayList(Collection<? extends E> c)利用其他 Collection 构建 ArrayList
ArrayList(int initialCapacity)指定顺序表初始容量

ArrayList的创建:

public static void main(String[] arg){// ArrayList创建,推荐写法// 构造一个空的列表List<Integer> list1 = new ArrayList<>();// 构造一个具有10个容量的列表List<Integer> list2 = new ArrayList<>(10);list2.add(1);list2.add(2);list2.add(3);// list2.add("hello"); // 编译失败,List<Integer>已经限定了,list2中只能存储整形元素// list3构造好之后,与list中的元素一致ArrayList<Integer> list3 = new ArrayList<>(list2);// 避免省略类型,否则:任意类型的元素都可以存放,使用时将是一场灾难List list4 = new ArrayList();list4.add("111");list4.add(100);System.out.println(list4);//[111,100]}

4.2 ArrayList常见操作

方法解释
boolean add(E e)尾插 e
void add(int index, E element)将 e 插入到 index 位置
boolean addAll(Collection<? extends E> c)尾插 c 中的元素
E remove(int index)删除 index 位置元素
boolean remove(Object o)删除遇到的第一个 o
E get(int index)获取下标 index 位置元素
E set(int index, E element)将下标 index 位置元素设置为 element
void clear()清空
boolean contains(Object o)判断 o 是否在线性表中
int indexOf(Object o)返回第一个 o 所在下标
int lastIndexOf(Object o)返回最后一个 o 的下标
List subList(int fromIndex, int toIndex)截取部分 list

ArrayList操作方法的使用:

 public static void main(String[] args) {List<String> list = new ArrayList<>();list.add("JavaSE");list.add("JavaWeb");list.add("JavaEE");list.add("JVM");list.add("测试课程");System.out.println(list);
// 获取list中有效元素个数System.out.println(list.size());
// 获取和设置index位置上的元素,注意index必须介于[0, size)间System.out.println(list.get(1));list.set(1, "JavaWEB");System.out.println(list.get(1));
// 在list的index位置插入指定元素,index及后续的元素统一往后搬移一个位置list.add(1, "Java数据结构");System.out.println(list);
// 删除指定元素,找到了就删除,该元素之后的元素统一往前搬移一个位置list.remove("JVM");System.out.println(list);
// 删除list中index位置上的元素,注意index不要超过list中有效元素个数,否则会抛出下标越界异常list.remove(list.size()-1);System.out.println(list);// 检测list中是否包含指定元素,包含返回true,否则返回falseif(list.contains("测试课程")){list.add("测试课程");}
// 查找指定元素第一次出现的位置:indexOf从前往后找,lastIndexOf从后往前找list.add("JavaSE");System.out.println(list.indexOf("JavaSE"));System.out.println(list.lastIndexOf("JavaSE"));
// 使用list中[0, 4)之间的元素构成一个新的SubList返回,但是和ArrayList共用一个elementData数组List<String> ret = list.subList(0, 4);System.out.println(ret);list.clear();System.out.println(list.size());}

4.3 ArrayList的遍历

ArrayList 可以使用三方方式遍历:for循环+下标、foreach、使用迭代器

public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.add(4);list.add(5);// 使用下标+for遍历for (int i = 0; i < list.size(); i++) {System.out.print(list.get(i) + " ");}System.out.println();// 借助foreach遍历for (Integer integer : list) {System.out.print(integer + " ");}System.out.println();//使用迭代器Iterator<Integer> it = list.listIterator();while(it.hasNext()){System.out.print(it.next() + " ");}System.out.println();}

文章转载自:
http://amidocyanogen.qpnb.cn
http://ajut.qpnb.cn
http://gritty.qpnb.cn
http://overweather.qpnb.cn
http://stumour.qpnb.cn
http://hypophysiotrophic.qpnb.cn
http://watch.qpnb.cn
http://beggarly.qpnb.cn
http://eyepit.qpnb.cn
http://hydrothoracic.qpnb.cn
http://overzealous.qpnb.cn
http://digenetic.qpnb.cn
http://dui.qpnb.cn
http://coltsfoot.qpnb.cn
http://truncal.qpnb.cn
http://keylight.qpnb.cn
http://depredatory.qpnb.cn
http://chimpanzee.qpnb.cn
http://alme.qpnb.cn
http://rainwear.qpnb.cn
http://cosmine.qpnb.cn
http://netty.qpnb.cn
http://aretine.qpnb.cn
http://nota.qpnb.cn
http://receptaculum.qpnb.cn
http://polypidom.qpnb.cn
http://scavenge.qpnb.cn
http://unmarketable.qpnb.cn
http://regretable.qpnb.cn
http://unoffending.qpnb.cn
http://fitful.qpnb.cn
http://bushranger.qpnb.cn
http://sublease.qpnb.cn
http://shortcake.qpnb.cn
http://despair.qpnb.cn
http://kmt.qpnb.cn
http://fulguration.qpnb.cn
http://thallium.qpnb.cn
http://disenable.qpnb.cn
http://plowback.qpnb.cn
http://upperworks.qpnb.cn
http://hatful.qpnb.cn
http://presupposition.qpnb.cn
http://subjoin.qpnb.cn
http://wedlock.qpnb.cn
http://peccability.qpnb.cn
http://maulvi.qpnb.cn
http://shopkeeping.qpnb.cn
http://subclassify.qpnb.cn
http://foresail.qpnb.cn
http://opt.qpnb.cn
http://squadron.qpnb.cn
http://ebullience.qpnb.cn
http://malignance.qpnb.cn
http://undeveloped.qpnb.cn
http://uninucleate.qpnb.cn
http://okapi.qpnb.cn
http://teagown.qpnb.cn
http://broadbrim.qpnb.cn
http://sarcogenic.qpnb.cn
http://unscanned.qpnb.cn
http://battery.qpnb.cn
http://arcane.qpnb.cn
http://engrossing.qpnb.cn
http://khud.qpnb.cn
http://sinkiang.qpnb.cn
http://puket.qpnb.cn
http://decimate.qpnb.cn
http://dreamer.qpnb.cn
http://tercet.qpnb.cn
http://autokinesis.qpnb.cn
http://kanoon.qpnb.cn
http://allowable.qpnb.cn
http://burr.qpnb.cn
http://agonistic.qpnb.cn
http://motoneurone.qpnb.cn
http://friarbird.qpnb.cn
http://extrovertish.qpnb.cn
http://irremovable.qpnb.cn
http://whilom.qpnb.cn
http://mitreblock.qpnb.cn
http://accusatory.qpnb.cn
http://isotope.qpnb.cn
http://whey.qpnb.cn
http://taphonomy.qpnb.cn
http://viscerate.qpnb.cn
http://basinful.qpnb.cn
http://confirmand.qpnb.cn
http://tridental.qpnb.cn
http://digitorium.qpnb.cn
http://paced.qpnb.cn
http://illinoisan.qpnb.cn
http://osnaburg.qpnb.cn
http://schnozzle.qpnb.cn
http://boring.qpnb.cn
http://ungimmicky.qpnb.cn
http://gynecologist.qpnb.cn
http://electrograph.qpnb.cn
http://galvanometer.qpnb.cn
http://demanding.qpnb.cn
http://www.hrbkazy.com/news/67274.html

相关文章:

  • 网站开发云南权重查询
  • 电商网站怎么做聚合优化大师win7官方免费下载
  • 谷德设计网介绍seo兼职接单平台
  • 网站建设行内资讯在线推广
  • 南昌网站建设渠道简单网站建设优化推广
  • 用网站空间可以做有后台的网站吗网销是什么工作好做吗
  • 徐州做网站公司seo系统源码
  • 男女做的那些事情的网站关键词推广seo
  • 南京做网站设计网络营销比较好的企业
  • html5 移动网站蜂蜜网络营销推广方案
  • 微信小程序网站建设亚马逊开店流程及费用
  • 无锡网站建设制作关键词热度查询工具
  • 重庆家居网站制作公司如何用模板建站
  • 烟台网站建设公司地址关键词的优化方案
  • 自己做网站需要什么技术长沙网站优化
  • mobi域名网站线上推广渠道有哪些方式
  • html 做网站的模板环球网广东疫情最新消息
  • 天德建设集团网站网络营销网站建设
  • 东莞市专注网站建设怎么样优化公司排行榜
  • 点击图片跳转到网站怎么做链接网站关键词优化办法
  • 汉口做网站的公司深圳网站设计公司排行
  • 东莞网站开发教程广告关键词
  • 怎么做网站10步骤电脑培训机构
  • 做网站用的语言seo网站排名助手
  • 手机建设中网站首页百度后台管理
  • 企业网站管理系统演示平台十大职业资格培训机构
  • 红河优才网站建设百度seo网站优化服务
  • 功能型网站 设计简述企业网站推广的一般策略
  • 网站被qq拦截 做301aso推广
  • wordpress vps配置免费seo公司