ArrayList

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

ArrayList

 
0
  #1
Oct 4th, 2009
Hi All,


Is it possible to convert an arraylist of chars to an array? For example:

ArrayList<Character> array = new ArrayList<Character>();

Can this be converted to an array of chars. I have tried using the toArray method but it is not working. Does anyone have any suggestions?

Thanks,
Kimjack
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 996
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: ArrayList

 
0
  #2
Oct 4th, 2009
toArray does work. There's something wrong with how you are trying to use it. Post your code.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 31
Reputation: mytime19 is an unknown quantity at this point 
Solved Threads: 1
mytime19 mytime19 is offline Offline
Light Poster

Re: ArrayList

 
0
  #3
Oct 4th, 2009
posting a snippet might solve the problem in your case.

Regards,
Lucky
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Re: ArrayList

 
0
  #4
Oct 4th, 2009
Here is a snippet of code:


Array<Character> letters = new ArrayList<Character>();
char[] characters =  new char[row * col];
...

public void createShuffleLetters()
	{
		//create letters
		for(char ch = 'a'; ch <='z'; ch++)
		{
			letters.add(ch);
		}
		//shuffle
		Collections.shuffle(letters);
		
		//convert arrayList to array
		letters.toArray(characters);		
	}

The line in red seems as if it should work, but I get the following error:
The method toArray(T[]) in the type ArrayList<Character> is not applicable for the arguments (char[])

I am trying to take an arraylist of characters and shuffle them then convert that arraylist to an array of chars. Any suggestions will be great.

Thanks,
Kimjack
Last edited by KimJack; Oct 4th, 2009 at 5:27 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 208
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: ArrayList

 
0
  #5
Oct 4th, 2009
You're trying to cast a Character object to its primitive char type, which doesn't work.
Try using an array of Character instead of char. =)
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Re: ArrayList

 
0
  #6
Oct 4th, 2009
hmm, I have never heard of an array of Character. Would you provide an example?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 208
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: ArrayList

 
0
  #7
Oct 4th, 2009
It's the same as a char array. You can make an array of any Object.
  1. Character[] ... = new Character[size];
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,625
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: ArrayList

 
0
  #8
Oct 5th, 2009
To elaborate on what llemes said (and he is correct), you are trying to use the following method "public Object[] toArray(Object[] a)" which takes an array of Objects, but you are passing in an array of chars. "char" is a primitive type, not an Object, which is why you are getting the error. On the other hand, the Character class (like every other class), "is-an" Object, so you can use that. Or you could simply use:

Character[] theChars = letters.toArray();
Out.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC