Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Replace an Existing Item in ArrayList

Learn to update or replace an existing element in ArrayList with a new specified element or value, using set (int index, Object newItem) method. 1. Replacing an Existing Item To replace an existing item, we must find the item’s exact position (index) in the ArrayList. Once we have …

Lokesh Gupta

January 12, 2023

Java ArrayList
Java ArrayList
ArrayList

Learn to update or replace an existing element in ArrayList with a new specified element or value, using set (int index, Object newItem) method.

1. Replacing an Existing Item

To replace an existing item, we must find the item’s exact position (index) in the ArrayList. Once we have the index, we can use set() method to update the replace the old element with a new item.

  • Find the index of an existing item using indexOf() method.
  • Use set(index, object) to update with the new item.

Note that the IndexOutOfBoundsException will occur if the provided index is out of bounds.

2. Example

The following Java program contains four strings. We are updating the value of “C” with “C_NEW”.

    ArrayList<String> list = new ArrayList<>(List.of("A", "B", "C", "D"));
    
    int index = list.indexOf("C");
    list.set(index, "C_NEW");
    
    Assertions.assertEquals("C_NEW", list.get(index));

    We can make the whole replacing process in a single statement as follows:

    list.set( list.indexOf("D") , "D_NEW");

    Happy Learning !!

    Read More:

    A Guide to Java ArrayList
    ArrayList Java Docs

    Comments

    Subscribe
    Notify of
    0 Comments
    Most Voted
    Newest Oldest
    Inline Feedbacks
    View all comments

    ArrayList Methods

    • ArrayList add()
    • ArrayList addAll()
    • ArrayList clone()
    • ArrayList contains()
    • ArrayList ensureCapacity()
    • ArrayList forEach()
    • ArrayList get()
    • Arraylist indexOf()
    • Arraylist lastIndexOf()
    • ArrayList listIterator()
    • ArrayList remove()
    • ArrayList removeAll()
    • ArrayList removeIf()
    • ArrayList retainAll()
    • ArrayList spliterator()
    • ArrayList subList()
    • ArrayList toArray()

    ArrayList Examples

    • Initialize Arraylist
    • Iteration
    • Add/replace Element
    • Add Multiple Elements
    • Check Empty List
    • Remove Element
    • Replace Element
    • Empty ArrayList
    • Synchronized ArrayList
    • Compare two lists
    • Remove duplicates
    • Merge two lists
    • Serialize a List
    • Swap two elements
    • Convert Array to ArrayList
    • Convert HashSet to ArrayList
    • Convert LinkedList to ArrayList
    • ArrayList vs LinkedList
    • ArrayList vs Vector
    Photo of author

    Lokesh Gupta

    A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
    Follow on Twitter Portfolio

    Previous

    Check if an ArrayList is Empty in Java

    Next

    Java ArrayList.ensureCapacity()

    About Us

    HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

    It also shares the best practices, algorithms & solutions and frequently asked interview questions.

    Tutorial Series

    OOP

    Regex

    Maven

    Logging

    TypeScript

    Python

    Meta Links

    About Us

    Advertise

    Contact Us

    Privacy Policy

    Our Blogs

    REST API Tutorial

    Follow On:

    • Github
    • LinkedIn
    • Twitter
    • Facebook
    Copyright © 2026 | Sitemap