list.add(int index, E element)和list.addAll(list1)
List.add(int index, E element):
在列表的指定位置插入指定元素(可选操作)。将当前处于该位置的元素(如果有的话)和所有后续元素向右移动(在其索引中加 1)。
参数:index - 要在其中插入指定元素处的索引。element - 要插入的元素
List.addAll(list1):
方法用于将指定 collection 中的所有元素添加到列表的尾部。如果 List 集合对象由于调用 addAll 方法而发生更改,则返回 true。
public static void main(String[] args){
List<String>list = new ArrayList<String>();
list.add("保护环境"); //向列表中添加数据
list.add("爱护地球"); //向列表中添加数据
list.add("从我做起"); //向列表中添加数据
list.add(1,"从我做起"); //在第1+1个元素的位置添加数据
List<String>list_ad = new ArrayList<String>();
list_ad.add("公益广告"); //将list中的全部元素添加到list_ad中
System.out.println("是否添加成功:"+list_ad.addAll(list)); //通过循环输出列表中的内容
for(int i=0;i<list_ad.size();i++){
System.out.println(i+":"+list_ad.get(i));
}
}
运行结果如下:
是否添加成功:true
0:公益广告
1:保护环境
2:从我做起
3:爱护地球
4:从我做起
list.add(int index, E element)和list.addAll(list1)的更多相关文章
- Java ArrayList add(int index, E element) example
Simple add() method is used for adding an element at the end of the list however there is another va ...
- ArrayList调用remove(int index)抛出UnsupportedOperationException问题分析以及解决记录
使用Arrays转数组成为List后,不能调用add(...)和remove(...)方法,此时如果调用就会抛出UnsupportedOperationException异常 原因 其实Arrays. ...
- public animal this[int index]|索引器的使用
学习如何使用索引器,索引器的使用是public 类型 this[int index]{get{};set{}} ,访问通过类的实例(对象)加[i], 例如animal[i],就像访问数组一样,其实就是 ...
- Character类的2个定义大小写方法以及charAt(int index)方法
API文档charAt(int index)是这样定义的: charAt(char index):Returns the char value at the specified index.在指定的索 ...
- 在包a中新建一个类A,在类A中有一个int add(int m)方法,用来求1+2+…+m 的和。在包b中新建一个类B,在类B中有一个int cheng(int n)方法,用来求n! 的结果。在包c中新建一个主类C,调用A、B中的方法输出1+2+…+30的和, 以及5!的计算结果。
package a; public class A { public void add(int m) { int sum=0; for (int i = 1; i <=m; i++) { sum ...
- 用c++语言编写函数 int index(char *s,char * t),返回字符串t在字符串s中出现的最左边的位置,如果s中没有与t匹配的子串,则返回-1。类似于索引的功能。
首先,分析一下程序的思路: 1:从s的第i个元素开始,与t中的第1个元素匹配,如果相等,则将s的第i+1元素与t中的第2个元素匹配,以此类推,如果t所有元素都匹配,则返回位置i;否则,执行2; 2: ...
- String的charAt(int index)可用于遍历一个字符串的所有字符
charAt(int index)遍历一个字符串的所有字符实例 String name = "Whatisjava?"; for (int i = 0; i < name.l ...
- MongoDB Sort op eration used more than the maximum 33554432 bytes of RAM. Add an index, or speci fy a smaller limit.
最近在获取mongodb某个集合的数据过程中,在进行排序的过程中报错,具体报错信息如下: Error: error: { , "errmsg" : "Executor e ...
- You can add an index on a column that can have NULL values if you are using the MyISAM, InnoDB, or MEMORY storage engine.
w https://dev.mysql.com/doc/refman/5.7/en/create-index.html MySQL :: MySQL 5.7 Reference Manual :: B ...
随机推荐
- 13.Linux文件存储系统
1.Linux 系统中的文件存储结构 Linux系统中常见的目录名称以及相应内容 2.系统内核中的udev 设备管理器会自动把硬件名称规范起来,目的是让用户通过设备文件的名字可以猜出设备大致的属性以及 ...
- IPSec 专题----转自华为文档
参考链接:https://support.huawei.com/enterprise/zh/doc/EDOC1000122878?section=j004 IPSec 特性全景 1.介绍 由于IP报文 ...
- ElasticSearch 入门简介
公号:码农充电站pro 主页:https://codeshellme.github.io ElasticSearch 是一款强大的.开源的.分布式的搜索与分析引擎,简称 ES,它提供了实时搜索与聚合分 ...
- Java链表(英雄增删查改)
链表(Linked List)介绍 链表是有序的列表,但是它在内存中是存储如下 小结: 1.链表是以节点的方式来存储,是链式存储. 2.每个节点包含 data 域, next 域:指向下一个节点. 3 ...
- TDH社区版搭建总结
在安装之前需要对docker分区进行格式化处理: Redhat/CentOS 在Redhat/CentOS上,docker分区必须采用XFS格式,实现的步骤如下: 1. 创建目录/var/lib/do ...
- Codeforces Round #628 (Div. 2) D. Ehab the Xorcist(异或,思维题)
题意: 寻找异或后值为 u,相加后和为 v 的最短数组. 思路: 异或得 u ,则 v 至少应大于等于 u ,且多出来的部分可以等分为两份相消. 即初始数组为 u , (v-u)/2 , (v-u)/ ...
- hdu3516 Tree Construction
Problem Description Consider a two-dimensional space with a set of points (xi, yi) that satisfy xi & ...
- python常用连接字符串
1.使用占位符% print(('%s%s%s' % ('one','two', 'three'))) 2.'+'号连接 字符串是不可变对象,每次改变会申请一块新的内存,操作符+连接字符串的时候会涉及 ...
- Django用户注册、登录
一.用户注册 1 ''' 2 注册的表单模型 3 forms.py 的例子 4 ''' 5 6 from django import forms #表单功能 7 from django.contrib ...
- React Native for Windows + macOS
React Native for Windows + macOS React Native 0.63 https://aka.ms/ReactNative https://microsoft.gith ...