Results 1 to 3 of 3
  1. #1
    plm-pusik is offline Member
    Join Date
    Aug 2010
    Posts
    17
    Rep Power
    0

    Default Arrays.asList(...) Problem

    Hi, I'm currently making a thing that counts how many times a word/sign is mentioned in a text and I've added a filter aswell. But I got some problems with my filter, this is how I initalize it:
    Java Code:
    public static List<String> FILTER = Arrays.asList(" ","!","talihow",".",",","-","_","'","kalle");
    And later when I want to add something to this list using this method:
    Java Code:
    for(int a=0; a<FILTER.size(); a++)
    	if(FILTER.get(a).length() == 1) {
    		FILTER.add(FILTER.get(a));
    		FILTER.remove(a);
    		ammCharSeperators++;
    	}
    It throws me an error saying:
    Java Code:
    Exception in thread "main" java.lang.UnsupportedOperationException
    	at java.util.AbstractList.add(AbstractList.java:131)
    	at java.util.AbstractList.add(AbstractList.java:91)
    	at WordCountClass.main(WordCountClass.java:27)
    I read a bit about the error and it seems like that when I use Arrays.asList(...) it creates a fixed List, which cannot be modified. Is this true? And if so, how can I create a list in a similar and still be able to modify it?

    Thanks.
    Last edited by plm-pusik; 09-17-2010 at 11:16 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by plm-pusik View Post
    I read a bit about the error and it seems like that when I use Arrays.asList(...) it creates a fixed List, which cannot be modified. Is this true? And if so, how can I create a list in a similar and still be able to modify it?
    Make a copy of that unmodifiable list; the ArrayList class has a constructor that takes another list to copy:

    Java Code:
    List copy= new ArrayList(Arrays.asList( ... ));
    kind regards,

    Jos

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    or

    Collections.addAll(FILTER," ","!","talihow",".",",","-","_","'","kalle");

Similar Threads

  1. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 02:18 PM
  2. loop problem-arrays
    By ester in forum New To Java
    Replies: 0
    Last Post: 02-02-2010, 09:44 PM
  3. about Arrays.asList source
    By geniusxiayi in forum Advanced Java
    Replies: 2
    Last Post: 04-13-2009, 07:12 PM
  4. Mergin two arrays problem.
    By frasifrasi in forum New To Java
    Replies: 4
    Last Post: 07-09-2008, 03:29 PM
  5. Arrays Problem (Advanced Java...Need Help)
    By Zebra in forum New To Java
    Replies: 9
    Last Post: 05-02-2008, 01:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •