java删除指定索引元素_将对象/元素添加到列表中的Java指定索引处
java删除指定索引元素
We have to create a List and add objects/elements to the List and given indexes in java.
我们必须创建一个List并将对象/元素添加到该List中,并在Java中添加给定的索引。
To add any object/element at given index to the List, we use, List.add() method - which is a method of List and used to inserts object/element at given index.
要将给定索引处的任何对象/元素添加到List,我们使用List.add()方法-这是List的一种方法,用于在给定索引处插入对象/元素。
Syntax:
句法:
List.add(index, element);
Here,
这里,
index is the position of the list, where we have to insert the element
index是列表的位置,我们必须在其中插入元素
element is the value or any object that is to be added at given index
element是要在给定索引处添加的值或任何对象
Example:
例:
L.add(0,10); //adds 10 at 0th index
L.add(1,20); //adds 20 at 1st index
L.add(3,30); //adds 30 at 2nd index
Program:
程序:
Here, we are creating a list of integer named int_list with some of the initial elements and we will add some of the elements at given indexes.
在这里,我们将创建一个名为int_list的整数列表, 其中包含一些初始元素,并将在给定索引处添加一些元素。
import java.util.*;
public class ListExample {public static void main (String[] args) {//creating a list of integers
List<Integer> int_list = new ArrayList<Integer> ();
//adding some of the elements
int_list.add (10);
int_list.add (20);
int_list.add (30);
int_list.add (40);
int_list.add (50);
//printing the empty List
System.out.print ("int_list= ");
System.out.println (int_list);
//adding elements by specifying index
int_list.add (0, 5); //will add 5 at 0th index
int_list.add (3, 7); //will add 7 at 3rd index
//printing thed list
System.out.print ("int_list= ");
System.out.print (int_list);
}
};
Output
输出量
int_list= [10, 20, 30, 40, 50]
int_list= [5, 10, 20, 7, 30, 40, 50]
翻译自: https://www.includehelp.com/java-programs/add-object-element-to-the-list-at-specified-index-in-java.aspx
java删除指定索引元素
java删除指定索引元素_将对象/元素添加到列表中的Java指定索引处相关推荐
- python中如何追加_如何在Python中将元素添加到列表中-追加,扩展和插入
在Python中使用列表时,您通常会希望向列表中添加新元素. Python列表数据类型具有三种添加元素的方法:append()-将单个元素追加到列表. extend() -将iterable的元素添加 ...
- scala集合中添加元素_如何在Scala中将元素添加到列表中?
scala集合中添加元素 In Scala, lists are immutable data structures in which adding new elements is not allow ...
- python list增加元素_将字符串的元素添加到列表中(python)
该程序设计用于获取由数字(任意长度)组成的字符串,并将该字符串的内容输出到列表中,一次一位数字.如果数字x小于或等于前面的数字y,则数字x将被插入到子列表中.在数字z大于y之前,x和z之间的所有内容都 ...
- python返回索引值_python 返回列表中某个值的索引方法
python 返回列表中某个值的索引方法 如下所示: list = [5,6,7,9,1,4,3,2,10] list.index(9) out:3 同时可以返回列表中最大值的索引list.index ...
- python list的extend (会将被插入的列表的每个元素从列表中拿出添加到列表中)与append方法(若被插入为列表,会将列表插入到源列表中)区别
python list的extend (会将被插入的列表的每个元素从列表中拿出添加到列表中)与append方法(若被插入为列表,会将列表插入到源列表中)区别 Python--list的extend() ...
- 列表删除前面两个元素_第015篇:List列表 - 课程二
列表的修改. 可以看到,我只做了一个操作,就是buy[3] = '红薯'.我给出了前后对比,前面从0数位置3是生菜,我修改后该元素变更为红薯. 当然也可以一次修改多个元素. 上面程序buy[1:3] ...
- 代码 删除的stach 找回_阿里大佬教你,如何写好 Java 代码!
点击上方蓝色链接,关注并"设为星标" Java干货,每天及时推送 阿里大佬分享的一篇很不错的文章,推荐收藏! 导读 明代王阳明先生在<传习录>谈为学之道时说: 私欲日生 ...
- elasticsearch删除索引_一文带您了解 Elasticsearch 中,如何进行索引管理(图文教程)
在 Elasticsearch 中,索引是一个非常重要的概念,它是具有相同结构的文档集合.类比关系型数据库,比如 Mysql, 你可以把它对标看成和库同级别的概念. 今天小哈将带着大家了解, 在 El ...
- java list 截取部分数据_Java List.subList()方法:获取列表中指定范围的子列表
集合类中的 List.subList() 方法用于获取列表中指定范围的子列表,该列表支持原列表所支持的所有可选操作.返回列表中指定范围的子列表. 语法: subList(int fromIndex,i ...
最新文章
- java c3p0 连接 mysql_Java使用C3P0数据源链接数据库
- 聚类算法小记(part2)--谱聚类
- junit5和junit4_JUnit 5 –条件
- VMware的屏幕太小
- JS:ES6-3 解构赋值与模板字符串
- jsp页面之间传中文参数显示乱码问题的解决
- SHA256算法详解及python实现
- 惠普打印机墨盒更换教程_惠普打印机安装步骤 打印机墨盒加墨方法
- 升余弦滤波器与无码间串扰(二)
- 身份证阅读器二次开发说明
- 高级程序员与初级程序员差别在哪里?
- 关键词抽取——结巴分词
- 2022牛客寒假算法基础集训营2
- oracle otm运输管理系统_OTM服务中国十年-Oracle.PDF
- 晨枫U盘维护V2.0_512M被淹死的鱼修正版
- 爬虫练习(2)-- 使用正则匹配爬取果壳问答
- 基于vue.js后台管理系统框架学习
- nil channel与closed channel的读写
- 自动发送微信消息或QQ消息的自用代码
- 收发一体超声波测距离传感器模块_为何能被行业广泛认可?超声波明渠流量计的行业秘密都在这里...