java删除指定索引元素_将对象/元素添加到列表中的Java指定索引处
java刪除指定索引元素
We have to create a List and add objects/elements to the List and given indexes in java.
我們必須創(chuàng)建一個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 indexL.add(1,20); //adds 20 at 1st indexL.add(3,30); //adds 30 at 2nd indexProgram:
程序:
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.
在這里,我們將創(chuàng)建一個名為int_list的整數(shù)列表, 其中包含一些初始元素,并將在給定索引處添加一些元素。
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 elementsint_list.add (10);int_list.add (20);int_list.add (30);int_list.add (40);int_list.add (50);//printing the empty ListSystem.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刪除指定索引元素
總結(jié)
以上是生活随笔為你收集整理的java删除指定索引元素_将对象/元素添加到列表中的Java指定索引处的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 杭州看内分泌性不孕最好的医院推荐
- 下一篇: 《幽兰》是哪个时期的作品?