944,085 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3257
  • Java RSS
Oct 4th, 2009
0

ArrayList

Expand Post »
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
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 4th, 2009
0

Re: ArrayList

toArray does work. There's something wrong with how you are trying to use it. Post your code.
Featured Poster
Reputation Points: 1938
Solved Threads: 953
Posting Expert
JamesCherrill is offline Offline
5,809 posts
since Apr 2008
Oct 4th, 2009
0

Re: ArrayList

posting a snippet might solve the problem in your case.

Regards,
Lucky
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
mytime19 is offline Offline
50 posts
since Dec 2006
Oct 4th, 2009
0

Re: ArrayList

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.
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 4th, 2009
0

Re: ArrayList

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. =)
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Oct 4th, 2009
0

Re: ArrayList

hmm, I have never heard of an array of Character. Would you provide an example?
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 4th, 2009
0

Re: ArrayList

It's the same as a char array. You can make an array of any Object.
Java Syntax (Toggle Plain Text)
  1. Character[] ... = new Character[size];
Reputation Points: 41
Solved Threads: 13
Posting Whiz in Training
llemes4011 is offline Offline
224 posts
since Aug 2008
Oct 5th, 2009
0

Re: ArrayList

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();
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Please help me with my method
Next Thread in Java Forum Timeline: Java syntax query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC