Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9248373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:55:54+00:00 2026-06-18T09:55:54+00:00

I am converting an empty String array (which i may get from middleware) to

  • 0

I am converting an empty String array (which i may get from middleware) to List .

For Conversion process i used Arrays.asList (Please see below code ) which is throwing an java.lang.UnsupportedOperationException .

public class Ramddd {
    public static void main(String args[]) {
        String[] words = null;
        if (words == null) {
            words = new String[0];
        }
        List<String> newWatchlist = Arrays.asList(words);
        List<String> other = new ArrayList<String>();
        other.add("ddd");
        newWatchlist.addAll(other);
    }

}



Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractList.add(Unknown Source)
    at java.util.AbstractCollection.addAll(Unknown Source)
    at Ramddd.main(Ramddd.java:18)

I dont get this Error if i use

List<String> mylist = new ArrayList<String>();
        for (int i = 0; i < words.length; i++) {
            mylist.add(words[i]);
        }

This forms an proper List and any operations like addALL , removeALL seems good , but dont want to go to this for loop approach , as it may result in performance .
Please let me know what is the best and safe approach for converting a String array to ArrayList .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:55:55+00:00Added an answer on June 18, 2026 at 9:55 am

    How about the following:

    public class Ramddd {
        public static void main(String args[]) {
            String[] words = getWords();
            if (words == null) {
                words = new String[0];
            }
            List<String> other = new ArrayList<String>(Arrays.asList(words));
            other.add("ddd");
        }
    }
    

    In terms of performance, I don’t this this is something to worry about, unless you have a really gigantic array of Strings.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following doubt concerning which exception to throw if list is empty
Converting to ODB.NET from System.Data.OracleClient and need help converting my connection string. Here is
When converting a project (in which a template method of IComparable was used a
Is there an easy way of converting a GAS string array (in .gs file)
When I am converting array of integers to array of string, I am doing
Converting json to oher type should be easy. From the Play! documentation : var
Is converting a file to a byte array the best way to save ANY
i am confused on why it doest not split the string? my array of
I'm currently converting some code from mysql to mysqli. I have downloaded this class:
Possible Duplicate: How do you convert Byte Array to Hexadecimal String, and vice versa?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.