Results 1 to 4 of 4
  1. #1
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Adding an Array to an Array

    Hi everyone,
    I have a question with adding an array to another array. I have an array of individual colors and add them all to one list; colorslist. Then I make a new list, a, and its equal to colorslist. Problem happens here. I want to add each individual color array to each array in a. But every time I try to do that I get this error message:

    "Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.AbstractList.add(AbstractList.java:131)
    at java.util.AbstractList.add(AbstractList.java:91)
    at SomethingNew.main(SomethingNew.java:35)"

    A feel as though I should do this a different way but not sure how. I've looked at various ways people have added things to lists but nothing that came close to do what I'm trying to do except what I have below. Any suggestions?

    Java Code:
          String [] red =  {"r"};
    	        String [] blue = {"u"};
    	        String [] green = {"g"};
    	        String [] yellow ={"y"};
    	        String [] white = {"w"};
    	        String [] black = {"b"};
    
    	        List<String[]> colorslist = Arrays.asList(red,blue,green,yellow,white,black);
    
    	        List<String[]> a = colorslist;
    	        String[] b = blue;
    	        String[] c  = green;
    	        String[] d  = yellow;
    	        String[] e  = white;
    	        String[] f  = black;
    
    	        for(String[]  s : a){
    	        	a.add(red);
    	        }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default Re: Adding an Array to an Array

    List<String[]> a = colorslist;

    Firstly, that does not give you 2 discrete Lists. It gives you 2 variables pointing at the same List object. Is that what you want?

    Secondly the Arrays.asList method returns a fixed length List. I'm not 100% sure but maybe you cannot add to the List.

  3. #3
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Adding an Array to an Array

    Yes that is what I wanted. Ultimately there would be six variables, including a, that point to that List object. But each one would add a different color to it. After having tried other ways, like Collections, this one seems, well almost, seemed to work best. And i think your right about it being fixed...

  4. #4
    clj89 is offline Member
    Join Date
    Oct 2011
    Posts
    14
    Rep Power
    0

    Default Re: Adding an Array to an Array

    I guess better yet, if this is more clear, how would I add an array to an array that is in an array, without getting an exception? Or that's not possible and I need to rethink this? I've tried using Collections, just an Array, ArrayList, but that error keeps coming up.

Similar Threads

  1. adding integers of an array
    By Lene90 in forum New To Java
    Replies: 9
    Last Post: 05-08-2011, 12:12 PM
  2. Replies: 1
    Last Post: 04-05-2011, 08:58 PM
  3. Array always adding to 0???
    By Javawizard in forum New To Java
    Replies: 6
    Last Post: 11-13-2010, 11:02 PM
  4. adding in array String
    By Mekonom in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 04:28 PM
  5. adding to an array
    By mayhewj7 in forum New To Java
    Replies: 14
    Last Post: 02-19-2009, 05:41 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
  •