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

网站生成app 免费工具线上推广公司

网站生成app 免费工具,线上推广公司,哪个网站可以免费设计房子,东莞市官网网站建设品牌参考笔记: java 单列集合List 万字详解(通俗易懂)_java singlelist-CSDN博客 目录 前言: 一、概述 二、特点 三、使用集合的经典四部曲 四、List接口常用的方法 五、List接口实现类——ArrayList 六、List接口实现类——Ve…

参考笔记: java 单列集合List 万字详解(通俗易懂)_java singlelist-CSDN博客


目录

前言:

一、概述

二、特点

三、使用集合的经典四部曲

四、List接口常用的方法

五、List接口实现类——ArrayList

六、List接口实现类——Vector

七、List接口实现类——LinkedList


前言:

         集合框架体系图如下:

        List 接口属于单列集合 Collection 接口下的其中一条分支,它最常用的三个实现类分别是——ArrayList、Vector、LinkedList 。这三个实现类各有各的特点,但又有着一些相同的特性。本文将从 List集合的介绍、特点、使用,再到它的三个常用实现类的底层源码分析,完完整整地梳理清楚

一、概述

List 位于 java.util.List ,是单列集合 Collection 接口下的一个子接口。接口不能直接被实例化,因此需要通过创建其子类对象来完成该接口的实例化(即多态)。List 接口最常用的 3  个子类有  ArrayList、Vector、LinkedList 。本文以 ArrayList 类为例演示 List 接口中的常用方法。eg:

        List list = new ArrayList();  (多态的方式保证了编译类型为 List 接口类型,运行类型为 ArrayList


二、特点

  •  List 集合是有序集合,即元素的存储顺序是一致的。 List 集合中的元素可通过索引的形式来访问

 "存储顺序一致" :比如将某个元素添加进集合时,它在索引为 3 的位置,也就是在当前集合中的第 4 个元素。那么,在当前集合无改动的情况下,你想取出该元素就必须从索引为 3 的位置去取,大白话就是:一个元素原来是在哪儿放的,现在就是从哪儿取的

  •  List 集合允许重复,即一个集合中可以有相同的元素

三、使用集合的经典四部曲

① 创建集合对象

② 创建元素对象

③ 将元素对象添加到集合对象中

 遍历集合

四、List接口常用的方法

 public boolean add(E e):将元素 e 添加到集合的末尾

② public void add(int index, E element):将元素添加到该集合索引为 index 的位置

③ public E remove(int index):将集合中索引为 index 的元素删除

④ public boolean remove(Object o):若该集合中存在该元素,则删除第一个指定的元素

⑤ public E get(int index):获取集合中索引为 index 的元素

⑥ public int indexOf(Object o):获取集合中第一个指定元素的索引,若集合中没有该元素,则返回 -1 

⑦ public int size():获取集合中的元素个数

⑧ public boolean contains(Object o):判断集合中是否有指定的元素

⑨ public boolean isEmpty():判断集合是否为空

⑩ public E set(int index, Object element):将集合中索引为 index 的元素修改为 element 

案例演示 

import java.util.ArrayList;
import java.util.List;
/*** 单链集合(Collection)之List集合:*      一.特点*          有序(元素的存取顺序一致), 可重复*      二.注意:*          List是接口,所以可以通过创建其子类ArrayList对象来完成该接口的实例化*          eg : List list = new ArrayList();*      三.List接口中的常用成员方法:*          1.public boolean add(E e)                   将元素 e 添加到集合的末尾*          2.public void add(int index, E element)     将元素 e 添加到该集合索引为 index 的位置*          3.public E remove(int index)                将集合中索引为 index 的元素删除*          4.public boolean remove(Object o)           若该集合中存在该元素,则删除第一个指定的元素*          5.public E get(int index)                   获取集合中索引为 index 的元素*          6.public int indexOf(Object o)              获取集合中第一个指定元素的索引,若集合中没有该元素,则返回-1*          7.public int size()                         获取集合中的元素个数*          8.public boolean contains(Object o)         判断集合中是否有指定的元素*          9.public boolean isEmpty()                  判断集合是否为空*          10.public E set(int index, Object element)  将集合中索引为index的元素修改为element**/
public class demo {public static void main(String[] args) {//1.创建集合对象List list = new ArrayList();/* 测试方法9:isEmpty(),判断此时集合是否为空 */boolean bool1 = list.isEmpty();System.out.println("集合此时为空吗:" + bool1);System.out.println("---------------------------------");//2.创建元素对象Student student1 = new Student("Kyrie", 33);Student student2 = new Student("Five", 22);Student student3 = new Student("Ice", 44);Student student4 = new Student("Cyan", 44);Student student5 = new Student("Cyan", 19);Student student6 = new Student("Cyan", 19);//3.将元素对象添加到集合对象中/* 测试方法1.add(E e)和2.add(int index, E element),对集合添加元素 */boolean b = list.add(student1);     //添加Kyrie到list集合中System.out.println("student1对象成功添加到集合list中了吗?" + b);System.out.println("---------------------------------");/* 对于方法1.add(E e)实际开发中我们都会省略前面的booelan b = ,而是直接调用方法 */list.add(student2);                 //添加Five到list集合中list.add(student3);                 //添加Ice到list集合中list.add(0, student4);        //添加第一个Cyan(44岁)到list集合中list.add(1, student5);        //添加第二个Cyan(19岁)到list集合中list.add(2, student6);        //添加第三个Cyan(19岁)到list集合中/* 测试一下list集合的可重复性 */list.add(10);list.add(10);/*本来student1,student2,student3,分别对应索引0,1,2但我们此处可以调用方法②将student4 ,student5 和 student6分别插入到索引为0,1,2的位置,这样,集合中的结果应该如下:索引 ----->  对应元素0   ----->   Cyan, 441   ----->   Cyan, 192   ----->   Cyan, 193   ----->   Kyrie, 334   ----->   Five, 225   ----->   Ice, 446   ----->   107   ----->   10*///4.遍历集合/* 测试方法5.get(int index), 遍历一下集合 */for (int i = 0; i < list.size(); ++i) {System.out.println("list集合的第" + i + "个元素为:" + list.get(i));}System.out.println("---------------------------------");/* 测试方法6.indexOf(Object o) */int index1 = list.indexOf(student4);System.out.println("student4元素在集合list中的索引为:" + index1);int index2 = list.indexOf(10);System.out.println("10的第一个元素在集合list中的索引为:" + index2);int index3 = list.indexOf(100);System.out.println("如果没有100这个元素,请返回-1" + index3);/* 测试方法7.size() */int length = list.size();System.out.println("添加了六个学生对象元素后,集合目前的长度为" + length);System.out.println("---------------------------------");/* 测试方法8.contains(Object) 和 再次测试方法9.isEmpty() */boolean b2 = list.isEmpty();System.out.println("添加了六个学生对象元素后,集合目前还为空吗?" + b2);System.out.println("---------------------------------");boolean b3_1 = list.contains("Cyan");System.out.println("集合list中有Cyan这个元素吗?" + b3_1);boolean b3_2 = list.contains(student1);System.out.println("集合list中有student1这个元素吗?" + b3_2);boolean b3_3 = list.contains(22);System.out.println("集合list中有22这个元素吗?" + b3_3);System.out.println("---------------------------------");/* 测试方法10.set(int index, Object o) */System.out.println("修改之前,list集合的索引为6的元素是:" + list.get(6));list.set(6, 11);System.out.println("修改之后,list集合的索引为6的元素是:" + list.get(6));System.out.println("修改之前,list集合的索引为5的元素是:" + list.get(5));list.set(5, student1);System.out.println("修改之后,list集合的索引为5的元素是:" + list.get(5));/* 测试方法3.remove(int index) 和 方法4.remove(Object o)*/list.remove(list.size() - 1);list.remove(student1);System.out.println("看看删除最后一个元素和第一个元素后集合成什么样子了:");for (int i = 0; i < list.size(); ++i) {System.out.println(list.get(i));}System.out.println("---------------------------------");}
}class Student {//私有的成员变量public String name;public int age;//公共的空参构造public Student() { }//公共的带参构造public Student(String name, int age) {this.name = name;this.age = age;}//重写toString() 方法,以便于遍历集合时可以打印出学生对象。@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

五、List接口实现类——ArrayList

        链接如下:

【Java集合】ArrayList源码深度分析-CSDN博客https://blog.csdn.net/m0_55908255/article/details/146886585?sharetype=blogdetail&sharerId=146886585&sharerefer=PC&sharesource=m0_55908255&spm=1011.2480.3001.8118        ArrayList主要内容包括:

        ①ArrayList类简介

        ②ArrayLIst类的底层实现

        ③ArrayList类的源码解读

六、List接口实现类——Vector

         链接如下:

【Java集合】Vector源码深度分析-CSDN博客https://blog.csdn.net/m0_55908255/article/details/146949740?sharetype=blogdetail&sharerId=146949740&sharerefer=PC&sharesource=m0_55908255&spm=1011.2480.3001.8118        Vector主要内容包括:

        ①Vector类简介

        ②Vector类的底层实现

        ③Vector类 VS ArrayLIst类

        ④Vector类的源码解读

七、List接口实现类——LinkedList

          链接如下:

【Java集合】Vector源码深度分析-CSDN博客https://blog.csdn.net/m0_55908255/article/details/146949740        LinkedList主要内容包括:

        ①LinkedList类简介

        ②LinkedList类的底层实现

        ③LinkedList类 VS ArrayList类

        ④LinkedList类源码解读

http://www.dtcms.com/wzjs/29459.html

相关文章:

  • 网站建设产品分割网络推广加盟
  • 哪里有免费招聘网站怎么利用互联网推广
  • 深圳手机网站制作百度平台订单查询
  • 做微商怎样加入网站卖东西赚钱线上推广方案怎么做
  • 奢侈品商城网站建设方案网络管理系统
  • wordpress主题出错武汉网站seo推广公司
  • 免费网站服务商网页快照
  • 在线充值网站怎么做关于软文营销的案例
  • 做网站职校选什么专业国外免费域名申请
  • 上海福州路附近做网站的公司seo自然排名优化
  • 网站公司 转型百度关键词挖掘工具爱站网
  • 关于校园网站的策划书百度网盘云资源搜索引擎
  • 搜索引擎网站推广法 怎么做百度客服在线咨询
  • 网站建设实用教程网域名查询地址
  • 湖州公司做网站怎样做市场营销策划
  • 站长工具排名分析制作网页多少钱
  • 注册私人网站软文怎么做
  • 淮北做网站电话合作seo公司
  • 宜春做网站的公司哪家好东莞网络营销推广公司
  • 建设银行企业网银网站打不开广州最新疫情通报
  • xz域名网站网络营销策划的内容
  • 南阳网站建设最全资源搜索引擎
  • 做日本ppt的模板下载网站制作网站的软件叫什么
  • 河源网站制作十大永久免费的软件下载
  • 东莞乐从网站建设微信朋友圈营销文案
  • 台州网站建设模板搜索引擎优化方式
  • 网站建设与维护实验报告百度网盘app官方下载
  • 网站截图怎么做seo营销工具
  • 24小时网站开发 pdf推广网站推广
  • 网站上做销售网点怎么做网络营销名词解释答案