Home > Java > javaTutorial > Why Does Removing Elements from `Arrays.asList` Result in an `UnsupportedOperationException`?

Why Does Removing Elements from `Arrays.asList` Result in an `UnsupportedOperationException`?

Patricia Arquette
Release: 2024-12-22 12:17:16
Original
930 people have browsed it

Why Does Removing Elements from `Arrays.asList` Result in an `UnsupportedOperationException`?

Resolving "UnsupportedOperationException" When Removing from "Arrays.asList" List

In the provided Java code, the exception "UnsupportedOperationException" occurs when attempting to remove an element from a "List" created using "Arrays.asList". This issue has multiple root causes:

Fixed-Size List from "Arrays.asList"

"Arrays.asList" returns a "List" backed by the provided array. This "List" is fixed-size, meaning its structure cannot be modified by adding or removing elements. Therefore, calling the "remove" method results in the "UnsupportedOperationException".

Regex Issue in "split" Method

The "split" method with the given parameter "|" treats it as a regular expression metacharacter. To split on a literal "|", it must be escaped as "|".

Improved Algorithm for Random Element Removal

To avoid calling "remove" multiple times with random indices, a more efficient approach is to generate enough random numbers within the range and remove elements at the corresponding indices in a single pass through the "List" using a "listIterator". This optimization reduces the complexity of the algorithm to O(N).

Revised Code

To resolve the issue and implement the improved algorithm, the following code can be used:

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public static String SelectRandomFromTemplate(String template, int count) {
    String[] split = template.split("\|");
    List<String> list = new LinkedList<String>(Arrays.asList(split));
    Random r = new Random();
    int[] indicesToRemove = new int[list.size() - count];

    for (int i = 0; i < indicesToRemove.length; i++) {
        indicesToRemove[i] = r.nextInt(list.size());
    }

    indicesToRemove = Arrays.sort(indicesToRemove);
    java.util.ListIterator<String> iter = list.listIterator();
    for (int i : indicesToRemove) {
        iter.next();
        iter.remove();
    }

    return StringUtils.join(list, ", ");
}
Copy after login

This revised code addresses the issues mentioned above, uses a fixed-size "List" created from an array and implements a more efficient algorithm for random element removal to avoid the "UnsupportedOperationException".

The above is the detailed content of Why Does Removing Elements from `Arrays.asList` Result in an `UnsupportedOperationException`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template