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指定索引处相关推荐

  1. python中如何追加_如何在Python中将元素添加到列表中-追加,扩展和插入

    在Python中使用列表时,您通常会希望向列表中添加新元素. Python列表数据类型具有三种添加元素的方法:append()-将单个元素追加到列表. extend() -将iterable的元素添加 ...

  2. scala集合中添加元素_如何在Scala中将元素添加到列表中?

    scala集合中添加元素 In Scala, lists are immutable data structures in which adding new elements is not allow ...

  3. python list增加元素_将字符串的元素添加到列表中(python)

    该程序设计用于获取由数字(任意长度)组成的字符串,并将该字符串的内容输出到列表中,一次一位数字.如果数字x小于或等于前面的数字y,则数字x将被插入到子列表中.在数字z大于y之前,x和z之间的所有内容都 ...

  4. python返回索引值_python 返回列表中某个值的索引方法

    python 返回列表中某个值的索引方法 如下所示: list = [5,6,7,9,1,4,3,2,10] list.index(9) out:3 同时可以返回列表中最大值的索引list.index ...

  5. python list的extend (会将被插入的列表的每个元素从列表中拿出添加到列表中)与append方法(若被插入为列表,会将列表插入到源列表中)区别

    python list的extend (会将被插入的列表的每个元素从列表中拿出添加到列表中)与append方法(若被插入为列表,会将列表插入到源列表中)区别 Python--list的extend() ...

  6. 列表删除前面两个元素_第015篇:List列表 - 课程二

    列表的修改. 可以看到,我只做了一个操作,就是buy[3] = '红薯'.我给出了前后对比,前面从0数位置3是生菜,我修改后该元素变更为红薯. 当然也可以一次修改多个元素. 上面程序buy[1:3] ...

  7. 代码 删除的stach 找回_阿里大佬教你,如何写好 Java 代码!

    点击上方蓝色链接,关注并"设为星标" Java干货,每天及时推送 阿里大佬分享的一篇很不错的文章,推荐收藏! 导读 明代王阳明先生在<传习录>谈为学之道时说: 私欲日生 ...

  8. elasticsearch删除索引_一文带您了解 Elasticsearch 中,如何进行索引管理(图文教程)

    在 Elasticsearch 中,索引是一个非常重要的概念,它是具有相同结构的文档集合.类比关系型数据库,比如 Mysql, 你可以把它对标看成和库同级别的概念. 今天小哈将带着大家了解, 在 El ...

  9. java list 截取部分数据_Java List.subList()方法:获取列表中指定范围的子列表

    集合类中的 List.subList() 方法用于获取列表中指定范围的子列表,该列表支持原列表所支持的所有可选操作.返回列表中指定范围的子列表. 语法: subList(int fromIndex,i ...

最新文章

  1. java c3p0 连接 mysql_Java使用C3P0数据源链接数据库
  2. 聚类算法小记(part2)--谱聚类
  3. junit5和junit4_JUnit 5 –条件
  4. VMware的屏幕太小
  5. JS:ES6-3 解构赋值与模板字符串
  6. jsp页面之间传中文参数显示乱码问题的解决
  7. SHA256算法详解及python实现
  8. 惠普打印机墨盒更换教程_惠普打印机安装步骤 打印机墨盒加墨方法
  9. 升余弦滤波器与无码间串扰(二)
  10. 身份证阅读器二次开发说明
  11. 高级程序员与初级程序员差别在哪里?
  12. 关键词抽取——结巴分词
  13. 2022牛客寒假算法基础集训营2
  14. oracle otm运输管理系统_OTM服务中国十年-Oracle.PDF
  15. 晨枫U盘维护V2.0_512M被淹死的鱼修正版
  16. 爬虫练习(2)-- 使用正则匹配爬取果壳问答
  17. 基于vue.js后台管理系统框架学习
  18. nil channel与closed channel的读写
  19. 自动发送微信消息或QQ消息的自用代码
  20. 收发一体超声波测距离传感器模块_为何能被行业广泛认可?超声波明渠流量计的行业秘密都在这里...

热门文章

  1. URL模块之parse方法
  2. node中的缓存机制
  3. html 语义化标签拾遗
  4. Vue2.0 --- vue-cli脚手架中全局引入JQ
  5. gopacket 在 windows 上面遇到的问题
  6. android-线程池-最顺手的写法
  7. SCREEN屏幕编程时候必须保证SCREN中词典的字段格式必须和数据表中字段的类型长度一致!...
  8. fortran语法笔记
  9. const 和readonly
  10. SAP MM/FI 自动过账实现 OBYC 接口执行