954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ArrayList

Hi All,


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

ArrayList array = new ArrayList();

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

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

posting a snippet might solve the problem in your case.

Regards,
Lucky

mytime19
Junior Poster in Training
50 posts since Dec 2006
Reputation Points: 10
Solved Threads: 3
 

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 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

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 

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

llemes4011
Posting Whiz in Training
224 posts since Aug 2008
Reputation Points: 41
Solved Threads: 13
 

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

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 

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

Character[] ... = new Character[size];
llemes4011
Posting Whiz in Training
224 posts since Aug 2008
Reputation Points: 41
Solved Threads: 13
 

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

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You