Results 1 to 4 of 4
Thread: Adding an Array to an Array
- 04-04-2012, 04:15 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
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); }
- 04-04-2012, 04:38 AM #2
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.
- 04-04-2012, 04:45 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
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...
- 04-04-2012, 06:52 AM #4
Member
- Join Date
- Oct 2011
- Posts
- 14
- Rep Power
- 0
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
-
adding integers of an array
By Lene90 in forum New To JavaReplies: 9Last Post: 05-08-2011, 01:12 PM -
Adding all elements of an Array together, not adding up correctly
By Teclis in forum New To JavaReplies: 1Last Post: 04-05-2011, 09:58 PM -
Array always adding to 0???
By Javawizard in forum New To JavaReplies: 6Last Post: 11-14-2010, 12:02 AM -
adding in array String
By Mekonom in forum New To JavaReplies: 4Last Post: 12-10-2009, 05:28 PM -
adding to an array
By mayhewj7 in forum New To JavaReplies: 14Last Post: 02-19-2009, 06:41 AM
Bookmarks