list赋值方法add()...和set()简介

news2024/9/27 23:30:54

目录

一、方法展示 

二、add() 方法介绍 

2.1.add(E element)

2.1.1源码 

 2.1.2.实例截图

2.1.3.Null引起的题外话 

2.2.add(int index, E element)

2.2.1.源码

 2.2.2.示例截图

2.2.3. add()引起IndexOutOfBoundsException简介

三、addAll()方法介绍

 3.1.addAll

3.1.1.源码 

 3.1.2.代码示例

 3.1.3.运行截图

四、set(int index,E element) 

4.1.元素替换方法:set(int index,E element)

4.1.1.源码 


一、方法展示 

         由上图可以看出,Java中线性表List接口的插入方法大概分为三大类,add、addAll和set。

二、add() 方法介绍 

2.1.add(E element)

2.1.1源码 

    /**
     * Appends the specified element to the end of this list (optional
     * operation).
     * 将指定的元素追加到此list的末尾(可选操作)
     *
     * <p>Lists that support this operation may place limitations on what
     * elements may be added to this list.  
     * <p>支持此操作的列表可能会限制元素可以被添加到该list中。
     *
     * In particular, some lists will refuse to add null elements, and others will         
     * impose restrictions on the type of elements that may be added.  
     * 特别是一些list将拒绝添加null元素,其他列表将强制对可以添加的元素类型的限制。
     * 
     * List classes should clearly specify in their documentation any restrictions
     * on what elements may be added.
     * List类应该在其文档中明确规定任何限制关于可以添加哪些元素。
     *
     * @param e element to be appended to this list
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     * @throws UnsupportedOperationException if the <tt>add</tt> operation
     *         is not supported by this list
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws NullPointerException if the specified element is null and this
     *         list does not permit null elements
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this list
     */

    boolean add(E e);

         简单来说,此方法有三个要素:将新元素添加到list末尾、只能传入指定类型元素、部分list拒接添加null。

        但据我所知,JDK中应该是没有原生的"NULL判断"来拒绝null的添加。

 2.1.2.实例截图

2.1.3.Null引起的题外话 

        Collection规范指出,当collection不支持null时,应引发NullPointerException。戳》》》菜鸟教程Interface Collection<E>  但是,此操作不是必须的,所以看自己选择。

2.2.add(int index, E element)

2.2.1.源码

    /**
     * Inserts the specified element at the specified position in this list
     * (optional operation).  
     * 在此列表中的指定位置插入指定的元素(可选操作)。
     * 
     * Shifts the element currently at that position (if any) and any subsequent 
     * elements to the right (adds one to their indices).
     * 将当前位于该位置的元素(如果有)和任何后续元素向右移动(在其索引中添加一个)。
     *
     * @param index index at which the specified element is to be inserted
     * @param element element to be inserted
     * @throws UnsupportedOperationException if the <tt>add</tt> operation
     *         is not supported by this list
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws NullPointerException if the specified element is null and
     *         this list does not permit null elements
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this list
     * @throws IndexOutOfBoundsException if the index is out of range
     *         (<tt>index &lt; 0 || index &gt; size()</tt>)
     */

    void add(int index, E element);

 2.2.2.示例截图

  

         当①执行时,下标为1的元素A,将原本list中的元素2,3,4,5都给向后移动了。

        当②和③执行时,新添加了元素B,但是出现了IndexOutOfBoundsException(下标越界异常),由此可以看出,当使用 add(int index, E element) 时,需要满足两个条件之一:当前下标已经存在元素,或当前下标的前一个位置存在元素。

        ==》当list中没有元素时,add的下标就需要从0开始,否则也会出现下标越界问题。

2.2.3. add()引起IndexOutOfBoundsException简介

         待补充...

三、addAll()方法介绍

 3.1.addAll

3.1.1.源码 

    /**
     * Appends all of the elements in the specified collection to the end of
     * this list, in the order that they are returned by the specified
     * collection's iterator (optional operation).  
     * 按照指定集合的迭代器返回的顺序,将指定集合中的所有元素追加到此列表的末尾(可选操作)。
     *
     * The behavior of this operation is undefined if the specified collection is         
     * modified while the operation is in progress.  (Note that this will occur if the
     * specified collection is this list, and it's nonempty.)
     * 如果在操作进行期间修改了指定的集合,则此操作的行为是未定义的。
     * (请注意,如果指定的集合就是这个list,它不是空的。)
     *
     * @param c collection containing elements to be added to this list
     * @return <tt>true</tt> if this list changed as a result of the call
     * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
     *         is not supported by this list
     * @throws ClassCastException if the class of an element of the specified
     *         collection prevents it from being added to this list
     * @throws NullPointerException if the specified collection contains one
     *         or more null elements and this list does not permit null
     *         elements, or if the specified collection is null
     * @throws IllegalArgumentException if some property of an element of the
     *         specified collection prevents it from being added to this list
     * @see #add(Object)
     */

    boolean addAll(Collection<? extends E> c);

        由上述源码可知,addAll的两个方法与add的使用大体相同,都是在原list内添加或在原list指定位置添加,同时下标取值范围 不能符合(index < 0 || index > size()),不过

        1、add添加的是元素,addAll添加的是 Collection<? extends E> c

        2、add使用时,部分list中不允许添加null,但还是能在list中添加null元素的;

              addAll则不允许添加null,但可以添加null元素

 3.1.2.代码示例

package com.task.test;

import java.util.ArrayList;
import java.util.List;

public class T1 {

    public static void main(String[] args) {

        List<String> list1 = new ArrayList<>();
        new T1().returnList1(list1);

        List<String> list2 = new ArrayList<>();
        new T1().returnList2(list2);

        List<String> list3 = new ArrayList<>();
        new T1().returnList3(list3);

        List<String> list4 = new ArrayList<>();

        List<String> list5 = null;


        List<String> listAll = new ArrayList<>();
        listAll.addAll(list1);
        System.out.println("1:listAll = " + listAll);
        listAll.addAll(list2);
        System.out.println("2:listAll = " + listAll);
        listAll.addAll(5,list3);
        System.out.println("3:listAll = " + listAll);
        listAll.addAll(list4);
        System.out.println("4:listAll = " + listAll);
        listAll.addAll(list5);
        System.out.println("5:listAll = " + listAll);
    }

    public void returnList1(List list){
        list.add("A1");
        list.add("B1");
        list.add("C1");
        list.add("D1");
        list.add("E1");
        list.add("F1");
        list.add("G1");
    }
    public void returnList2(List list){
        list.add("A2");
        list.add("B2");
        list.add("C2");
        list.add("D2");
        list.add("E2");
        list.add("F2");
        list.add("G2");
    }

    public void returnList3(List list){
        list.add(null);
    }
}

 3.1.3.运行截图

         关于list4 和 list5 的区别,可以查看》》》ArrayList初始化的小知识

四、set(int index,E element) 

4.1.元素替换方法:set(int index,E element)

4.1.1.源码 

    /**
     * Replaces the element at the specified position in this list with the
     * specified element (optional operation).
     * 将此列表中指定位置的元素替换为指定的元素(可选操作)。
     *
     * @param index index of the element to replace
     * @param element element to be stored at the specified position
     * @return the element previously at the specified position
     * @throws UnsupportedOperationException if the <tt>set</tt> operation
     *         is not supported by this list
     * @throws ClassCastException if the class of the specified element
     *         prevents it from being added to this list
     * @throws NullPointerException if the specified element is null and
     *         this list does not permit null elements
     * @throws IllegalArgumentException if some property of the specified
     *         element prevents it from being added to this list
     * @throws IndexOutOfBoundsException if the index is out of range
     *         (index < 0 || index >= size())
     */

    E set(int index, E element);

        1、这是个元素替换方法。

        2、使用set时的下标index 取值范围不能在(index < 0 || index >= size())之间。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/671452.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CSS查缺补漏之《媒体查询@media与BFC》

示例代码如下&#xff1a; <div class"box">世界你好</div> .box {width: 200px;height: 200px;background-color: yellow;color: white;font-size: 24px;text-align: center;line-height: 200px; } 媒体类型 媒体类型允许指定文件将如何在不同媒体上…

OpenCV——《图像平滑》结果输出对比《形态学操作》

1.图像平滑 图像平滑是一项简单且使用频率很高的图像处理方法&#xff0c;可以用来压制、弱化或消除图像中的细节、突变、边缘和噪声&#xff0c;最常见的是用来减少图像上的噪声。 常用的滤波器主要为&#xff1a; 均值滤波器&#xff1a;并不能完全消除噪声&#xff0c;只能…

【服务器】搭建hMailServer 服务实现远程发送邮件

typora-copy-images-to: upload hMailServer 是一个邮件服务器,通过它我们可以搭建自己的邮件服务,通过cpolar内网映射工具即可实现远程发送邮件,不需要使用公网服务器,不需要域名,而且邮件账号名称可以自定义. 下面以windows 10系统为环境,介绍使用方法: 1. 安装hMailServe…

小鹏G9高压平台800V电驱动实拍

近日&#xff0c;小鹏汽车董事长何小鹏在其个人社交账号上透露&#xff0c;小鹏G9正按原定计划按部就班推进节奏&#xff0c;将于8月启动预订&#xff0c;9月正式迎来上市&#xff0c;上市后很快就会启动用户交付。 图片来源&#xff1a;何小鹏官方微博 需要样件请联&#xff1…

Oracle DMP文件导入

dmp文件可以在Navicat中的 把dmp放入其中。然后用数据泵导入。遗憾的是报错 [ERR] ORA-39001: invalid argument value [ERR] ORA-39000: bad dump file specification [ERR] ORA-39143: dump file "/xxx.DMP" may be an original export dump file 改为imp工具&…

干货分享 | TSMaster标定模块自动化控制应用指南

本文目录&#xff1a; 一、TSMaster标定模块自动化控制的基础原理 1.1 TSMaster的标定系统变量 1.2 内部TSMaster调用C脚本实现标定模块的自动化控制 1.3 外部调用COM组件实现自动化标定 二、标定自动化控制场景与TSMaster实例 2.1 C脚本实现控制标定模块的启动与关闭的设…

Goby 漏洞发布|网神SecGate 3600防火墙 sys_export_conf_local_save 文件读取漏洞

漏洞名称&#xff1a;网神SecGate 3600防火墙 sys_export_conf_local_save 文件读取漏洞 English Name&#xff1a;Weaver OA PluginViewServlet Authentication Bypass Vulnerability CVSS core: 8.0 影响资产数&#xff1a;738 漏洞描述&#xff1a; 网神SecGate 3600防…

同一 tomcat 不同项目 session 共享实现

说明 这里仅讨论 同一个tomcat&#xff0c;部署了两个工程&#xff08;两个war包&#xff09;。不涉及不同tomcat,不涉及集群 背景 tomcat中的工程A包含用户登录、退出、权限控制等功能&#xff1b;工程B包含业务功能接口。工程A将用户登录信息加密响应给前端&#xff0c;前…

基于Java校园快递代取系统设计实现(源码+lw+部署文档+讲解等)

博主介绍&#xff1a; ✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战 ✌ &#x1f345; 文末获取源码联系 &#x1f345; &#x1f447;&#x1f3fb; 精…

NLP(五十五)LangChain入门

LangChain简介 背景 由于ChatGPT的发行&#xff0c;大模型&#xff08;Large Language Model, LLM&#xff09;已经变得非常流行了。也许你可能没有足够的资金和计算资源从头开始训练大模型&#xff0c;但你仍然可以使用大模型来做一些比较酷的事情&#xff0c;比如&#xff1…

大数据开发薪资怎么样

想必想入行的小伙伴在正式开始学习之前&#xff0c;都会考虑薪资这个可观因素。有不少小伙伴是看中了大数据的薪资选择加入这个行业的&#xff0c;想知道以后能找什么工作以及工作薪水&#xff0c;那不妨让我们以数据的方式来展示一下~ 猎聘大数据研究院发布了《2022未来人才就…

制造执行系统(MES)的核心功能是什么?

制造执行系统&#xff08;MES&#xff09;的核心功能是什么? 01 什么是MES 制造执行系统&#xff08;MES&#xff09;是一种用于监控、控制和优化制造过程的软件系统。它通过与企业资源计划&#xff08;ERP&#xff09;系统和自动化系统的集成&#xff0c;实现对生产过程的管…

小红书品牌营销策略分析!品牌方必看

小红书在品牌营销方面的策略非常成功&#xff0c;特别是在口碑营销、内容营销和小红书素人达人种草营销方面的运用。以下是伯乐网络传媒对这些策略的详细分析&#xff0c;想要做小红书营销推广的商家必看&#xff01; 一、口碑营销 对于小红书APP来说,口碑营销的传播主体就是小…

Android build.gradle文件

一、ABI&#xff08;Application Binary Interface&#xff09;应用程序二进制接口 其实ABI可以不设置&#xff0c;这样编译时&#xff0c;就会将项目里所有依赖资源包里的so库都打到最终的apk里。 但是&#xff0c;ABI支持多的话&#xff0c;apk也会大&#xff0c;所以一般只支…

MySQL数据库——存储引擎

MySQL数据库——存储引擎 一、MySQL存储引擎1.存储引擎的概念2.常用存储引擎3.存储引擎的分类4.企业选择存储引擎依据 二、MyISAM 存储引擎1 MyISAM的相关了解2 MyISAM的特点3 MyISAM表支持3种不同的存储格式4.MyISAM适用的生产场景 三、InnoDB 存储引擎1 InnoDB的相关了解2 In…

探索NDK和逆向工程在Android开发中的力量

NDK是什么&#xff1f; NDK&#xff08;Native Development Kit&#xff09;是一组工具集&#xff0c;用于在Android平台上开发和构建使用C或其他本地语言编写的应用程序。NDK提供了一些库和工具&#xff0c;使开发人员能够在应用中使用本地代码&#xff0c;并实现与Java代码的…

ant+svn项目打包部署错误记录

安装ant可以参考下这个 http://t.csdn.cn/kx1ZX 第一个错误&#xff0c;原因是缺少ant-contrib.jar导致&#xff0c;将对应jar包放入ant的lib下即可 [taskdef] Could not load definitions from resource net/sf/antcontrib/antcontrib.properties. It could not be found. B…

低/无代码平台:公民开发和公民自动化,让人人成为软件开发者

在企业数字化转型领域&#xff0c;公民自动化是一个备受关注的概念。虽然这个词汇可能有些“陌生又熟悉”&#xff0c;但在公民开发运动中已经引起了不小的轰动。那么&#xff0c;什么是公民自动化&#xff1f;它与公民开发之间又有哪些不同&#xff1f; 公民开发是什么&#x…

ATTCK 红日靶场(三)-简记

Step 1-》信息收集 端口 nmap -p -A -sV 192.168.1.110 目录 dirsearch -u 192.168.1.110 --exelude-status 400,401,403,404,501,503 访问 ip/1.php -->根目录、禁用函数 ip/robots.txt !!!------- IP/administrator-->>joomla-cms IP/configuration.php~--&…

利用uniapp创建移动端项目

目录 申请微信开发者账号 一、特殊声明 二、申请微信开发者账号 创建小程序项目 一、引言 二、创建小程序工程 三、uni-app框架简介 初识uniapp项目 一、uni-app工程目录结构 二、创建练习页面 创建登录页面 一、拷贝所有素材文件 二、创建登陆页面 创建注册页面 …