Add value at specified index/position in the list
- In the given ArrayList, we can add value at the specified index/position by using the set method.
- Set(int index,Object element) method will take 2 parameters.
- Used to replaces the element at the specified position in this list with the specified element
Parameters:
index - index of the element to replace
element - element to be stored at the specified position
Set in ArrayList |
Program:
import java.util.ArrayList;
import java.util.List;
public class setArrayList {
public static void main(String[] args) {
List l = new ArrayList<>();
l.add(0);
l.add(1);
l.add(2);
l.add(3);
l.add(4);
System.out.println("elements in list before insert value at specified postion=" + l);
l.set(3, 5);
System.out.println("elements in list after insert value at specified postion=" + l);
}
}
Output:
Add value at specified index in list |
Please comment below to feedback or ask questions.
No comments:
Post a Comment
Please comment below to feedback or ask questions.