Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Java programs » Java list programs

Add object/element to the List at specified index in Java



Here, we are going to learn how to add object/element to the list at specified index in java?
Submitted by IncludeHelp, on October 11, 2018

We have to create a List and add objects/elements to the List and given indexes in 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.

Syntax:

    List.add(index, element);

Here,

  • index is the position of the list, where we have to insert the element
  • element is the value or any object that is to be added at given index

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.

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]





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.