Wednesday, 3 November 2021

Java: How to replace a value in ArrayList or LinkedList

There are multiple ways to replace a value in ArrayList or LinkedList.

 

Approach 1: Using set method of List interface.

 

E set(int index, E element)

Replaces the element at the specified position in this list with the specified element.

 

ListReplaceElement.java

package com.sample.app.collections;

import java.util.ArrayList;
import java.util.List;

public class ListReplaceElement {

	public static void main(String args[]) {
		List<Integer> primes = new ArrayList<>();
		primes.add(2);
		primes.add(3);
		primes.add(5);

		System.out.println("primes -> " + primes);

		System.out.println("\nSetting the value at index 1 to 7\n");
		primes.set(1, 7);

		System.out.println("primes -> " + primes);

	}

}

 

Output

primes -> [2, 3, 5]

Setting the value at index 1 to 7

primes -> [2, 7, 5]

 

Approach 2: Using set method of ListIterator. This approach is useful while you want to replace the value while iterating.

 

Example

 

ListIterator<Integer> listIterator = primes.listIterator();
while (listIterator.hasNext()) {
	int val = listIterator.next();
	if (val == 3) {
		listIterator.set(7);
		break;
	}
}

Find the below working application.

 

ListReplaceElementUsingIterator.java

package com.sample.app.collections;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class ListReplaceElementUsingIterator {

	public static void main(String args[]) {
		List<Integer> primes = new ArrayList<>();
		primes.add(2);
		primes.add(3);
		primes.add(5);

		System.out.println("primes -> " + primes);

		System.out.println("\nReplacing the value 3 by 7\n");
		ListIterator<Integer> listIterator = primes.listIterator();
		while (listIterator.hasNext()) {
			int val = listIterator.next();
			if (val == 3) {
				listIterator.set(7);
				break;
			}
		}

		System.out.println("primes -> " + primes);

	}

}


Output

primes -> [2, 3, 5]

Replacing the value 3 by 7

primes -> [2, 7, 5]


Approach 3: Remove the element at specific index and add new element using add method.

 

Step 1: Remove element at the index.

Ex: primes.remove(1);

 

Step 2: Add new element at the index.

Ex: primes.add(1, 7);

 

Find the below working application.

 

ListReplaceElement1.java

package com.sample.app.collections;

import java.util.ArrayList;
import java.util.List;

public class ListReplaceElement1 {

	public static void main(String args[]) {
		List<Integer> primes = new ArrayList<>();
		primes.add(2);
		primes.add(3);
		primes.add(5);

		System.out.println("primes -> " + primes);

		System.out.println("\nReplacing the value 3 by 7\n");
		
		//Remove element at index 1
		primes.remove(1);
		//Add new element at the index 1
		primes.add(1, 7);

		System.out.println("primes -> " + primes);

	}

}


Output

primes -> [2, 3, 5]

Replacing the value 3 by 7

primes -> [2, 7, 5]

 

 

You may like

Interview Questions

Java: How to find maximum element in a collection?

Java: How to find the minimum element in a collection?

Java: Get the index of first occurrence of a substring

Java: Get the index of last occurrence of a substring

Java: Get all the indexes of occurrence of a substring

No comments:

Post a Comment