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

news/2025/3/15 16:25:58/

目录

一、方法展示 

二、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.ppmy.cn/news/510484.html

相关文章

漏刻有时数据可视化Echarts组件开发(27):端午地图粽情之你的家乡吃甜还是吃咸?

端午地图粽情之你的家乡吃甜还是吃咸&#xff1f; 前言Echarts创意来源Echarts核心代码1.引入外部文件2.构建HTML容器3.Echarts组件开发预置各省数据初始化DOM配置选项geo组件series组件自适应浏览器完整option选项配置代码 前言 中国各地对粽子的口味偏好存在一定的差异&…

苹果手机查看mysql_教你苹果手机怎么查几个月或多天以前的通话记录

原标题&#xff1a;教你苹果手机怎么查几个月或多天以前的通话记录 苹果手机会保存我们最近的通话记录&#xff0c;但你仔细查看后会发现&#xff0c;保存的通话记录条数是有限的&#xff0c;iPhone手机上只会显示最近100条通话记录&#xff0c;在这100条以前的通话记录在手机上…

苹果6严重卡顿_这样优化手机才能保持流畅,苹果安卓手机都适用

文/ 排版/ 对于现在的智能手机来说&#xff0c;已经远非以前的手机可比&#xff0c;拥有大内存&#xff0c;强大的处理器&#xff0c;可以说在用户使用流畅度方面是很不错的&#xff0c;但是不论是安卓手机还是苹果手机&#xff0c;在使用时间久了后&#xff0c;都是比较卡顿的…

苹果cms去掉链接index.php,如何去除苹果cms链接中的index.php

这篇文章主要为大家详细介绍了如何去除苹果cms链接中的index.php&#xff0c;具有一定的参考价值&#xff0c;感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。 官方默认的网站模式是动态模式&#xff0c;动态模式下链接中自带有“index.php”想要去除网站链接…

PHP服务端 苹果支付(IAP)处理

公司做的app需要做IAP订阅支付&#xff0c;开始觉得和微信的支付流程差不多&#xff0c;做起来还是有点麻烦&#xff0c;主要是网上的文章很少&#xff0c;不能拿来主义。自己做完总结一下&#xff0c;希望对小伙伴们有帮助我就很欣慰了。代码写的不好 不要喷我。。。 首先讲一…

一个画布有多个子图_如何把多张图拼成一张长图

如何把多张图拼成一张长图 相信很多小伙伴都有在使用苹果iPhone手机&#xff0c;在其中如何才能通过微信拼接多张截图呢&#xff1f;方法很简单&#xff0c;下面小编就来为大家介绍。 具体如下&#xff1a; 1. 首先&#xff0c;打开手机上的“微信”。进入首页后&#xff0c;点…

苹果计算机 win10,苹果怎么装win10苹果装win10详细教程【图文】

现如今越来越多的人在选择电子产品时都特别青睐苹果的产品,像苹果的手机,平板电脑以及笔记本电脑等,都是人们争相购买的产品。与同价位的产品相比较,苹果的产品在使用上更加方便,且安全性更出色。但是由于与人们常用的系统有所不同,在使用上一些人还是有些不习惯,尤其是…

计算机里的MAC,怎么在mac苹果电脑中查看电脑开机运行的时长

怎么在mac苹果电脑中查看电脑开机运行的时长 腾讯视频/爱奇艺/优酷/外卖 充值4折起 今天给大家介绍一下怎么在mac苹果电脑中查看电脑开机运行的时长的具体操作步骤。 1. 打开电脑,进入桌面,点击左上角的苹果图标。 2. 在打开的菜单中,选择‘关于本机’选项,点击。 3. 进入新…