Skip to content

[Edit] Java: .set() #7097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 102 additions & 14 deletions content/java/concepts/array-list/terms/set/set.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,149 @@
---
Title: '.set()'
Description: 'Replaces the element present in a specified position in an ArrayList.'
Description: 'Replaces the element present at a specified position with another element in an ArrayList.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Arrays'
- 'Data Types'
- 'Index'
- 'Methods'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

The **`.set()`** method replaces an element at a specified position with another element in an `ArrayList` instance. After execution, the replaced element is returned.
In Java, the **`.set()`** [method](https://www.codecademy.com/resources/docs/java/methods) replaces the element present at a specified position with another element in an `ArrayList`. After execution, the method returns the replaced element.

Understanding how `.set()` works is crucial when dealing with mutable lists in Java, especially when updating or modifying elements during processing.

## Syntax

```pseudo
arrayListInstance.set(index, newElement);
arrayList.set(index, element);
```

An element at a specified `index` is replaced in an `arrayListInstance` with the `newElement`.
**Parameters:**

- `index`: The position in the list where the element should be replaced.
- `element`: The new element that will replace the old one at the given index.

**Return value:**

> **Note:** The `newElement` must be of the same [data type](https://www.codecademy.com/resources/docs/java/data-types) as the rest of the elements in `arrayListInstance`. Otherwise, an error will occur.
The `.set()` method returns the replaced element.

## Example
> **Note:** The `element` must be of the same [data type](https://www.codecademy.com/resources/docs/java/data-types) as the rest of the elements in the `arrayList`. Otherwise, an [error](https://www.codecademy.com/resources/docs/java/errors) will occur.

In the example below, an empty `ArrayList` instance `studentList` is created and can hold `String`-type elements. Next, a few elements are added with the [`.add()`](https://www.codecademy.com/resources/docs/java/array-list/add) method. Lastly, two students are replaced with the `.set()` method:
## Example 1: Basic Usage of `.set()`

This example uses the `.set()` method to replace elements at multiple positions in an `ArrayList`:

```java
import java.util.ArrayList;

public class Students {
public static void main(String[] args) {

// Create an ArrayList
ArrayList<String> studentList = new ArrayList<String>();

// Add new values to the studentList
// Add values to the ArrayList
studentList.add("John");
studentList.add("Lily");
studentList.add("Samantha");
studentList.add("Tony");

// `.set()` method returns replaced element's value
// Replace elements at multiple positions in the ArrayList
String replacedStudentOne = studentList.set(1, "David");
String replacedStudentTwo = studentList.set(2, "George");

// Output updated ArrayList and replaced elements
// Print the updated ArrayList
System.out.println("Updated ArrayList: " + studentList);
System.out.println("Replaced Elements: " + replacedStudentOne + " and " + replacedStudentTwo);
}
}
```

The output should look like this:
Here is the output:

```shell
Updated ArrayList: [John, David, George, Tony]
Replaced Elements: Lily and Samantha
```

## Example 2: Using `.set()` with Integers

This example uses the `.set()` method to replace an integer with another integer in an `ArrayList`:

```java
import java.util.ArrayList;

public class Example2 {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<Integer> numbers = new ArrayList<>();

// Add values to the ArrayList
numbers.add(10);
numbers.add(20);
numbers.add(30);

// Replace the second element
numbers.set(1, 99);

// Print the updated ArrayList
System.out.println(numbers);
}
}
```

Here is the output:

```shell
[10, 99, 30]
```

## Example 3: Handling Index Errors

This example attempts to replace an element at an out-of-bounds index using the `.set()` method, resulting in an `IndexOutOfBoundsException`:

```java
import java.util.ArrayList;

public class Example3 {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> colors = new ArrayList<>();

// Add values to the ArrayList
colors.add("Red");
colors.add("Green");

try {
colors.set(5, "Blue");
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
```

In this example, the `try...catch` block is used to catch the exception and display the error message.

Here is the output:

```shell
Error: Index 5 out of bounds for length 2
```

## Frequently Asked Questions

### 1. Can `.set()` be used to add new elements to a list?

No. The `.set()` method replaces an existing element at a given index. To insert a new element, use `.add()` instead.

### 2. Is `.set()` available for all collections in Java?

No. `.set()` is only available for classes that implement the `List` interface, such as `ArrayList`, `LinkedList`, and `Vector`.

### 3. Does `.set()` change the size of the list?

No. `.set()` only modifies the value at the specified index.