1

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
    That will make it a little better: List<String> mylist = new ArrayList<String>(words.length); Commented Feb 6, 2013 at 23:09

2 Answers 2

1

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.

3
  • the response is dynamic one , i cant know earlier if its null or contains anything . Commented Feb 6, 2013 at 23:03
  • sometimes the array a Gigantic one ( 150 words) ( so dont want to affect the application performance .) Commented Feb 6, 2013 at 23:04
  • 2
    I would say a Gigantic array would be in the millions of records. A size of 150 is very small. Anyways, you should always rely on empirical evidence when optimizing performance. Don't prematurely optimize! Commented Feb 6, 2013 at 23:05
1

The method java.util.Arrays.asList(T...) returns a fixed-size list backed by the specified array. This implementation of List for this method (java.util.Arrays.ArrayList) don't have support for these methods. See the documentation for the java.util.AbstractList.

If you know the total size of your list of words, you can init the capacitity for ArrayList, adding n elements requires O(n) time. If you don't know the final size, use LinkedList.

See more in List Implementations (The Java Tutorials > Collections > Implementations).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.