This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert set to char[]?  RSS feed

 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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[].
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Bob Zoloman
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!