Results 1 to 3 of 3
Thread: Hash set
- 07-20-2011, 06:06 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 24
- Rep Power
- 0
Hash set
I have an array 'arr' with integers. I need to display only the unique values. I did this using hash array set and While executing I found an error like this ,
Exception in thread "main" java.lang.ArrayStoreException: [I
at java.util.AbstractCollection.toArray(AbstractColle ction.java:171)
at test.main(test.java:57)
Here is my logic I used to get unique values,
Set hashSet = new HashSet(Arrays.asList(arr));
String[] unique = (String[]) hashSet.toArray(new String[hashSet.size()]);
Can someone help me on this??
- 07-20-2011, 06:42 PM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
The toArray() method of the collection classes works somewhat unintuitively. The correct syntax is:
Java Code:String[] unique = new String[hashSet.size()]; hashSet.toArray(unique);
- 07-20-2011, 06:58 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
hash table
By pinkfahtema in forum New To JavaReplies: 1Last Post: 03-28-2011, 08:25 AM -
hash map program
By katturv in forum New To JavaReplies: 1Last Post: 12-04-2010, 05:01 PM -
Hash
By sandy1028 in forum New To JavaReplies: 4Last Post: 04-17-2009, 10:36 AM -
Hash Map
By rekha in forum New To JavaReplies: 1Last Post: 03-21-2009, 01:00 PM -
Hash Table help
By rhm54 in forum New To JavaReplies: 0Last Post: 02-08-2008, 01:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks