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

Replace an element at given index of a List with new element in Java



Here, we are going to learn how to replace an element at given index of a list with a new element in java?
Submitted by IncludeHelp, on October 10, 2018

Given a list of the integers and we have to replace it an element from specified index with a new element in java.

To replace an element in the list - we use List.set() method.

List.set() method

List.set() method replaces an existing element at given index with new element and returns the old object/element.

Syntax:

    Object List.set(index, new_object);

Example:

    Input:
    List = [10, 20, 30, 40, 50]
    Index =1 
    new_element = 1000

    Output:
    Updated list =[10, 1000, 30, 40, 50]

Program:

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) ;

		//replace elements
		int old_ele, new_ele, index;
		index =0; new_ele=12;
		old_ele = int_list.set (index, new_ele);
		System.out.println (old_ele+" is replace with "+new_ele);

		index =2; new_ele=24;
		old_ele = int_list.set (index, new_ele);
		System.out.println (old_ele+" is replace with "+new_ele);

		//printing updated list 
		System.out.println ("updated list: " + int_list);
	}
};

Output

10 is replace with 12
30 is replace with 24
updated list: [12, 20, 24, 40, 50]


Another Example: Replace all elements of EVEN indexes with 0 and ODD indexes by 1

Example:

    Input:
    List = [10, 20, 30, 40, 50]

    Output:
    Updated list =[0, 1, 0, 1, 0]

Program:

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) ;
		
		System.out.println ("len: " + int_list.size ());
		//replace elements 
		for (int i=0; i<int_list.size(); i++) {
			if (i%2==0)
				int_list.set (i, 0);
			else
				int_list.set (i, 1);		
		}
		
		//printing updated list 
		System.out.println ("updated list: " + int_list);
	}
};

Output

len: 5
updated list: [0, 1, 0, 1, 0]





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.