| | |
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,647
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
Views: 295 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
account android api apple applet application arguments array arrays automation binary bluetooth calculator chat class classes client code component data database draw eclipse error errors event exception file filechooser fractal game givemetehcodez google graphics gui helpwithhomework homework html ide image inheritance input integer j2me java javaprojects jlabel jmf jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception number object open-source oracle print problem program programming project property recursion ria scanner screen search server set size sms socket sort sourcelabs splash sql sqlite static string support swing test testautomation threads time transfer tree windows






