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

toArray does work. There's something wrong with how you are trying to use it. Post your code.

posting a snippet might solve the problem in your case.

Regards,
Lucky

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

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. =)

hmm, I have never heard of an array of Character. Would you provide an example?

It's the same as a char array. You can make an array of any Object.

Character[] ... = new Character[size];

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();