| | |
ArrayList
![]() |
•
•
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: 205
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,580
Reputation:
Solved Threads: 199
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 |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner sell server set size sms sort sql string swing terminal threads tree web websites windows






