• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using toArray(Object[]) in java 5

 
Tim Fernandez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I hope someone can answer me about this.



These lines of code should work and names should now contain the data from the the set. My question though is how can I use generics in this case because when i use the last the line below i get this warning:
Type safety: The method toArray(Object[]) belongs to the raw type
Set. References to generic type Set<E> should be parameterized.
. How can i remove this warning?I can remove it using the toArray(). But how can i remove it using toArray(Object [])
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

The warning is due to the fact that the Set you defined isn't typed (using Generics). Since you mentioned Generics, I'm guessing you already have your Map defined similar to the following:



If that's the case, then the keySet() method should be returning a Set<String>. But you are only saving it into an untyped Set reference. By the time you use that Set in the last line, the compiler can't tell it's actually a Set<String>. It can't be sure the operation you're asking for is going to be legal, so it issues the warning.

Try this - it compiles for me:




Hope this helps,
-- Jon
 
Tim Fernandez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon
Thanks for the reply. I did what you said and now the toArray(Object[]) works but i get a warning in this line:



the warning says:

Type safety: the expression of type Set needs unchecked conversion to conform to Set<String>

tim
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

I'm not able to test drive it right now to be sure, but it sounds like the problem might be that you haven't defined your map as I'd assumed. In order for the Map.keySet() method to return a Set<String> instead of Set (untyped), you need to define the Map as typed, something like I gave in the earlier post:



Whatever the first item is in the Map type (shown in bold above) is the type of the Keys.... the other is the type of the Values. So, if you want the Keys to be known to be Strings, you have to define the Map as shown. If you don't know any common supertype for your Values, you can use Object.

-- Jon
 
Tim Fernandez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon,

I did define the types when i created the Map. But still i got the warning.
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim,

Can you post the line(s) where you declare and instantiate the Map? Also, if you then pass the map to a method, show how you accept the Map parameter? You could have problems in all three of these areas, if all three are not specified with Generics.

I don't have any theories outside this one, so I was hoping this would be it ;-)

-- Jon
 
Tim Fernandez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jon,


Below is the code:



tia,
tim
 
Kevin Conaway
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

I'm late to the party here, but if your code snippet is accurate, in getKeys(), you are using 'map' instead of 'fieldMap'. If this is an artifact of copy/paste then feel free to ignore me. Otherwise, 'map' could be defined elsewhere in your code and could be causing this problem.

Kevin
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, what I think Kevin is saying, and what I was getting at in my last post, is the kind of problems shown here. Once anything is assigned to an un-typed reference, no guarantees remain about the types:



I was trying to convey a few different places where you coud lose the type, sorry if the examples got too jumbled.

If there isn't any point where the Map is referenced without being typed, I don't know of any other reason why you would see that error.

If there's more to the code that explains how "fieldMap" becomes "map", and you aren't sure it's ok, post again with more of the places it's touched/passed around/returned from a method call/stored.

-- Jon
[ February 21, 2006: Message edited by: Jon Egan ]
 
Tim Fernandez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin and Jon,

fieldMap is a typo. It should have been map.
And yes i was able to fix it. Thanks Jon for the last post it really cleared things out.

thanks,
tim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic