• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer
  • Java Tutorial
  • Java Posts
  • Node.js
  • Spring Core
  • Algorithms
  • Docker
  • Blogging
  • Misc
Tech Stack Journal

Tech Stack Journal

[Solved] Why List.add throws UnsupportedOperationException in Java?

February 20, 2021 by Admin Leave a Comment

Contents

  • 1 What is a fixed-length object?
  • 2 Example
  • 3 So, How to fix this UnsupportedOperationException?

If you are getting an UnsupportedOperationException while adding an object to List the reason could be that you are performing this operation on a fixed-size List object.

What is a fixed-length object?

You must have used Arrays.asList method to convert an array object to List object. If you closely look at the documentation of Arrays.asList method, it says that, Arrays.asList method returns a fixed-size list backed by the specified array, which means that the returned List is of a fixed size. You cannot change the structure as you do to a normal List. That is, you cannot add or remove elements to this List. This List object has a direct relation with the array from which it is created. Any change you make to the object within the List will get updated within the array also. Similarly, any change you make to the array object will get reflected in the List object as well.

Example

Below example will result in UnsupportedOperationException as it is trying to add new element to a fixed-size List object.

package com.techstackjournal;

import java.util.Arrays;
import java.util.List;

public class ArrayToList {

	public static void main(String[] args) {

		String[] arr = { "Alpha", "Beta", "Gamma" };

		List<String> list = Arrays.asList(arr);

		list.add("Theta");

		for (Object object : list) {
			System.out.println(object);
		}

	}

}

Output:

Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.AbstractList.add(AbstractList.java:153)
	at java.base/java.util.AbstractList.add(AbstractList.java:111)
	at com.techstackjournal.ArrayToList.main(ArrayToList.java:14)

So, How to fix this UnsupportedOperationException?

If your objective is to copy elements from array to List and add further elements to the List, then the approach to copy the elements from array to List using Arrays.asList is not correct. You should better go for other alternative approaches, which I explained in my “How to Convert an Array to List in Java?” post.

Filed Under: Java

Previous Post: « How to Convert an Array to List in Java?
Next Post: Arrays.asList in Java Examples »

Primary Sidebar

More to See

Factorial Program in JavaScript using While Loop

March 24, 2021 By Admin

Flowchart of Nested For Loop

March 22, 2021 By Admin

Secondary Sidebar

Categories

  • Algorithms
  • Blogging
  • Docker
  • Java
  • JavaScript
  • Misc
  • Node.js
  • Programming Concepts
  • Spring Core
  • Windows

Archives

  • April 2021 (1)
  • March 2021 (7)
  • February 2021 (6)
  • January 2021 (1)
  • December 2020 (1)
  • September 2020 (2)
  • August 2020 (5)
  • July 2020 (4)
  • June 2020 (1)
  • May 2020 (4)
  • April 2020 (22)
  • November 2019 (3)
  • September 2019 (2)
  • August 2019 (6)

Footer

  • Privacy Policy
  • Cookie Policy
  • Contact Us
  • About Us

Recent

  • How to Fix – The system cannot find the file C:\ProgramData\Oracle\Java\javapath\java.exe
  • Factorial Program in JavaScript using While Loop
  • Flowchart of Nested For Loop
  • Difference Between Constructor Overloading and Method Overloading in Java
  • Constructor Calling in Java

Search

Copyright © 2021 · Tech Stack Journal · Log in