1.Basic java Example program to replace an element at specified index java arrayList.
Output:
- package com.instanceofjavaforus;
- import java.util.ArrayList;
- public class ReplaceArrayList{
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList<Integer> arrayList = new ArrayList<Integer>();
- //Add elements to Arraylist
- arrayList.add(1);
- arrayList.add(2);
- arrayList.add(3);
- arrayList.add(4);
- arrayList.add(5);
- arrayList.add(6);
- arrayList.add(7);
- arrayList.add(8);
- arrayList.add(9);
- arrayList.add(10);
- /*
- To replace an element at the specified index of ArrayList use
- Object set(int index, Object obj) method.
- This Object set(int index, Object obj) method replaces the specified element at the specified
- index in the ArrayList and returns the element previously at the specified position.
- */
- arrayList.set(1,23);
- System.out.println("ArrayList contains...");
- Iterator itr=arrayList.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
Output:
- ArrayList contains...
- 1
- 23
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Bagikan
Basic Java example program to replace an element at specified index arraylist
4/
5
Oleh
Kris Kimcil