| | |
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,625
Reputation:
Solved Threads: 205
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
android api appinventor apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet compiler component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jni jpanel julia linux list login loop loops mac map method methods mobile netbeans newbie number online oracle page parameter print problem program programming project recursion reporting rotatetext scanner screen server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working






