| Author |
How to convert set to char[]?
|
Bob Zoloman
Ranch Hand
Joined: Jul 02, 2006
Posts: 72
|
|
Hi, I am trying to convert a set to a char[] using the toarray method, but I can't seem to get it too work. Thanks in advance.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
By "set", I am assuming that you mean one of the collections. If I am wrong, ignore the rest... The toArray() method that takes a parameter, takes an Object array. A char array while an object is not an instance of Object array -- hence, you'll get a compile error. This is actually a good thing, because since a collection only takes Objects, it wouldn't be able to use a char array anyway. Henry [ September 16, 2006: Message edited by: Henry Wong ]
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Bob Zoloman
Ranch Hand
Joined: Jul 02, 2006
Posts: 72
|
|
|
I'm trying to convert a set collection to a char[]. Could someone show me how this is done? I thought it would be easy to convert an object[] array to a char array, but I'm not having any luck with this.
|
 |
Scott Johnson
Ranch Hand
Joined: Aug 24, 2005
Posts: 518
|
|
convert an object[] array to a char array
What does newKey contain? Strings? Characters? If you'll post a bit more code including where newKey is created and populated, we might be able to better understand what you are trying to do.
|
 |
Bob Zoloman
Ranch Hand
Joined: Jul 02, 2006
Posts: 72
|
|
I tried using the code below but I get a Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; error. What I'm doing: I'm taking a string keyword and converting it to a char array. I then create a linedhashset so I can remove duplicate characters by adding it to a set. Once the set is made I want to convert it back to a char array, so I use the toarray method and cast it to Character[]. When I try and loop through it gives me an excepetion.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
|
OK. The Set contains Character objects, not char's. So you can convert to a Character[] using toArray(); it will work fine. Then you'll have to use a for loop to turn it into a char[] if that's what you need -- copying one charValue() at a time.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Bob Zoloman
Ranch Hand
Joined: Jul 02, 2006
Posts: 72
|
|
I thought the set contained char[] primitive types. Anyways when I loop through my code above it throws an Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
I am actually surprised that you were able to even compile. A set can only hold objects, so it isn't able to hold chars. To allow it to compile, you need to change your for loop as such... Of course, now your set holds Character objects... but at least, it will compile. Henry [HW: Oops. Forgot about boxing. This post may be moot. See next post for explaination about the class cast exception] [ September 16, 2006: Message edited by: Henry Wong ]
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
The call to the toArray() method is also wrong. You need to call the version that will return an array type that you can cast. As such... Henry
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[Bob]: I thought the set contained char[] primitive types. And yet, it contains Character objects, in a Character[] array. A Collection can contain only reference types, never primitives, and thus the toArray() method can convert to an array of reference type, but never to primitive type. So how is it that you were able to add() primitive chars into this Set in the first place? You are evidently using JDK 5 (or possibly 6), which has a feature called autoboxing. When you have a primitive type but require a reference type, boxing converts the prmitive to an instance of the apprpriate wrapper class. (E.g. a char is converted to a Character.) Or when you have a wrapper object but need a primitive, unboxing can do the reverse. But Autoboxing and unboxing can never apply to converting entire arrays or collections at once. So while you can add() a chr (because the char is converted to a Character), you cannot toArray() to a char[] - that exceeds the capabilities of autoboxing and unboxing. You must convert the Set to a Character[], not a char[].
|
"I'm not back." - Bill Harding, Twister
|
 |
Bob Zoloman
Ranch Hand
Joined: Jul 02, 2006
Posts: 72
|
|
|
I'm using java 1.5 to compile. As for the code I'm completely confused, this stuff is way over my head.
|
 |
Garrett Rowe
Ranch Hand
Joined: Jan 17, 2006
Posts: 1295
|
|
|
Since you're using a raw Set, the run time type of the array returned by the toArray() method is an Object[]. It can't be cast to a Character[] because there is no guarantee that the array holds only Character objects. Since you are obviously using Java 1.5, (since you were able to autobox the chars) I would suggest using a generics and using a Set<Character>
|
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
|
 |
Bob Zoloman
Ranch Hand
Joined: Jul 02, 2006
Posts: 72
|
|
|
I think I'll try a whole new approach to this problem. I didn't realize this method would be so troublesome.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Originally posted by Bob Zoloman: I think I'll try a whole new approach to this problem. I didn't realize this method would be so troublesome.
Did you change the toArray() method call to the one that I suggested? The rest of the program looks fine, it is just this one line that is causing the class cast exception. Henry [ September 16, 2006: Message edited by: Henry Wong ]
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
[Garrett]: the run time type of the array returned by the toArray() method is an Object[] That depends which version of the code above you're looking at, since toArray() is overloaded, and both versions are used in the preceding text. newKey.toArray() : this returns an Object[], period. newKey.toArray(new Character[0]) : this returns a Character[] newKey.toArray(new char[0]) : this doesn't compile because the type of the argument must be an array of reference type. [Bob]: I think I'll try a whole new approach to this problem. I didn't realize this method would be so troublesome. It would be fairly simple at this point to just create an empty char[] array, and then populate it in a loop. Assuming you even need a char[] array, which may or may not be the case.
|
 |
Bob Zoloman
Ranch Hand
Joined: Jul 02, 2006
Posts: 72
|
|
This is the code I tried. It throws a lot of different errors... [ September 16, 2006: Message edited by: Bob Zoloman ] [ September 16, 2006: Message edited by: Bob Zoloman ] [ September 16, 2006: Message edited by: Bob Zoloman ]
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
Originally posted by Garrett Rowe: Since you're using a raw Set, the run time type of the array returned by the toArray() method is an Object[].
That has nothing to do with it being a raw Set. The method *always* returns an Object[] (because of erasure, it cannot use the generic type parameter to instanciate a more specific array).
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Bob Zoloman
Ranch Hand
Joined: Jul 02, 2006
Posts: 72
|
|
nvm it works! Just did a bad cut & paste job. Thanks for sticking it out with me, I appreciate all the help. [ September 16, 2006: Message edited by: Bob Zoloman ]
|
 |
 |
|
|
subject: How to convert set to char[]?
|
|
|