BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ArrayList set() Method

Last Updated: June 12, 2024 by Chaitanya Singh | Filed Under: java

Java ArrayList set() method is used to replace an existing element present in the ArrayList at the specified position with the new given element. The syntax of the set() method is:

public E set(int index, E element)

In this syntax:

  • index: This specifies the position of the element which needs to be replaced.
  • element: The new element which will replace the existing element.

Note: The method returns the element (old element) which is replaced.

Example

Let’s see how to use the set() method to replace an existing element in the ArrayList.

import java.util.ArrayList;

public class ArrayListExample {
public static void main(String[] args) {
// Create an ArrayList and add some elements
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
list.add("Mango");

System.out.println("Original list: " + list);

// Replacing the second element (element at index 1)
String oldElement = list.set(1, "Blueberry");
System.out.println("Replaced element: " + oldElement);
System.out.println("Updated list: " + list);
}
}

Output:

Original list: [Apple, Banana, Orange, Mango]
Replaced element: Banana
Updated list: [Apple, Blueberry, Orange, Mango]

In this example:

  1. We have an ArrayList of String type that contains the fruits.
  2. We have used set() method to replace second element (index 1) of the list.
  3. As we discussed earlier, this method returns the element which is getting replaced. In this case, it retuned element “Banana” as this is replaced with “Blueberry”.
  4. In the end, we printed the updated list with new element.

Example 2

In this example, we have an ArrayList of Integer type. Let’s replace the elements using set() method.

import java.util.ArrayList;
public class SetExample {
public static void main(String args[]) {
ArrayList<Integer> arraylist = new ArrayList<>();
arraylist.add(1);
arraylist.add(2);
arraylist.add(3);
arraylist.add(4);
arraylist.add(5);
arraylist.add(6);
arraylist.add(7);
System.out.println("ArrayList before update: "+arraylist);
//Updating 1st element
arraylist.set(0, 11);
//Updating 2nd element
arraylist.set(1, 22);
//Updating 3rd element
arraylist.set(2, 33);
//Updating 4th element
arraylist.set(3, 44);
//Updating 5th element
arraylist.set(4, 55);
System.out.println("ArrayList after Update: "+arraylist);
}
}

Output:

ArrayList before update: [1, 2, 3, 4, 5, 6, 7]
ArrayList after Update: [11, 22, 33, 44, 55, 6, 7]
❮ Java ArrayList

Top Related Articles:

  1. How to Find length of an Integer in Java
  2. Java Scanner class with examples
  3. Copy Elements of One ArrayList to Another ArrayList in Java
  4. How to Convert an array to ArrayList in java
  5. How to loop HashMap in java

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap