Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was implementing a program to remove the duplicates from the 2 character array. I implemented these 2 solutions, Solution 1 worked fine, but Solution 2 given me UnSupportedoperationException. I am wonderring why i sthat so?

The two solutions are given below;

public void getDiffernce(Character[] inp1, Character[] inp2){

// Solution 1:

    // **********************************************************************************

    List<Character> list1 = new ArrayList<Character>(Arrays.asList(inp1));
    List<Character> list2 = new ArrayList<Character>(Arrays.asList(inp2));
    list1.removeAll(list2);

    System.out.println(list1);
    System.out.println("*********************************************************************************");

// Solution 2:

    Character a[] = {'f', 'x', 'l', 'b', 'y'};
    Character b[] = {'x', 'b','d'};

    List<Character> al1 = new ArrayList<Character>(); 
    List<Character> al2 = new ArrayList<Character>(); 
    al1 = (Arrays.asList(a)); System.out.println(al1);
    al2 = (Arrays.asList(b)); System.out.println(al2);  
    al1.removeAll(al2);         // retainAll(al2);      
    System.out.println(al1);


    }
share|improve this question

closed as off-topic by thorsten müller, MichaelT, Joachim Sauer, unholysampler, Neil Jun 26 '13 at 15:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about specific programming problems encountered while writing code are off-topic, but can be asked on Stack Overflow." – thorsten müller, MichaelT, Joachim Sauer, unholysampler, Neil
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Welcome to Programmers. Please note that your question is better suited for StackOverflow. –  Uwe Plonus Jun 26 '13 at 15:04
2  
Duplicated on SO. –  Joachim Sauer Jun 26 '13 at 15:10
    
Consider using Sets instead of Lists for eliminating duplicates. –  Neil Jun 26 '13 at 15:11
1  
Please do not cross post across multiple sites. If something is off topic on one site, please use the 'flag' link and ask for it to be migrated to the correct site. –  MichaelT Jun 26 '13 at 15:12
    
@MichaelT Sure. I was unaware to post the question here or SO. Then Uwe recommended to post it in SO. I did that, by the time I came back question here was on hold. :) –  JNL Jun 27 '13 at 20:28

1 Answer 1

up vote 2 down vote accepted

The API doc for Arrays.asList() states:

Returns a fixed-size list backed by the specified array.

fixed-size = you can't remove or add anything.

Solution 1 works because it copies the content of the fixed-size list into a regular (variable-size) ArrayList.

share|improve this answer
    
al1.addAll((Arrays.asList(a))); al2.addAll((Arrays.asList(b))); The addAll() worked for me –  JNL Jun 27 '13 at 20:30
    
@JNL: because you initialize al1 and al2 with variable-size ArrayList instances, but in your "Solution 2" you then replace those with fixed-size lists (in lines 5 and 6). –  Michael Borgwardt Jun 27 '13 at 22:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.