| | |
ArrayList
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2006
Posts: 114
Reputation:
Solved Threads: 0
Here is a snippet of code:
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
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.
•
•
Join Date: Aug 2008
Posts: 208
Reputation:
Solved Threads: 13
It's the same as a char array. You can make an array of any Object.
Java Syntax (Toggle Plain Text)
Character[] ... = new Character[size];
•
•
Join Date: Sep 2008
Posts: 1,629
Reputation:
Solved Threads: 206
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();
Character[] theChars = letters.toArray();
Out.
![]() |
Similar Threads
- Using operator[] with an ArrayList (Java)
- Is ArrayList Better than Vector (Java)
- Storing Point2D.Double objects in an ArrayList. (Java)
- Need Help with ArrayList sorting (Java)
- Linked arraylist (Java)
Other Threads in the Java Forum
- Previous Thread: Please help me with my method
- Next Thread: Java syntax query
| Thread Tools | Search this Thread |
Tag cloud for Java
addressbook android api apple applet application arguments array arrays automation binary bluetooth button calculator chat class classes client code columns component converter database draw eclipse error errors event exception file fractal ftp game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inetaddress input integer j2me japplet java javaprojects jme jmf jni jpanel julia link linux list loop map method methods midlethttpconnection mobile netbeans newbie number objects openjavafx oracle php print problem program programming project projects recursion rim scanner screen server set signing size smart sms socket sort sql storm string support swing test threads time tree unlimited variablebinding webservices windows






