This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

merging multiple arrays, unchecked warnings

 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

I wrote a static method that merges multiple arrays.
The first argument is the type of array followed by
a variable argument list specifying the arrays to merge.

The method's code is:



The code works fine, but gives two "unchecked" warnings
in lines 15 and 33 of the above listing.

I know that a SuppressWarnings annotation would make
them go away, but I would really prefer what I call a "proper solution".

What am I missing here?

Any comments welcome.

Thanks for your time
Matt

 
Bhagat Singh Rawat
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually
Array.newInstance
returns Object Array, so it is unknown what the type is.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagat Singh Rawat wrote:Actually
Array.newInstance
returns Object Array, so it is unknown what the type is.


that's why I have to type cast it.

is there any better way way of doing it?

Or let me rephrase:
Is there any way of doing a type-safe cast?

Thanks
Matt
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the world of Java generics.

Maybe start here.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot David.

I've learned something new.

So this is as good as it gets

And my original question should have been:

Can I create an array whose component type is a type parameter?

Details can be found here
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhagat Singh Rawat wrote:Actually
Array.newInstance
returns Object Array, so it is unknown what the type is.

It returns Object since it can also be an int[] if the class is Integer.TYPE (the class to represent the int primitive).

Matt, sometimes you cannot avoid warnings like these. Your case is one of those cases; you know that the Object returned is an array of T so the cast is in fact safe. The compiler can't verify though so that's why the warning is there. You can use the SuppressWarnings("unchecked") annotation. Keep it as local as possible:
By the way, you can move the creation of result outside the if-statement. No matter what value count has, you always call "Array.newInstance(clazz, count);". If count happens to be 0 then you just hard code that 0.

Ok, there is actually the case where the total array size would exceed Integer.MAX_VALUE so count wraps around and gets negative; in that case you may want to set count to Integer.MAX_VALUE itself.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,

the code posted is not the original code.
I reorganized so I would not have to explain to much.

Also, if the first element in he first array is not null
and not empty, Class<T> clazz is not needed at all.




I understand that introducing a new variable and suppress warnings
on its creation will eliminate the need to supress warnings on the
entire method.

Thing is that I don't know what the garbage collector makes out
of that as this method will be used heavily with huge amount of
data.

I know thta theoretically the GC will clean out the object once it
has gone out of scope, but in real life and under heavy load it
mostly is different.

Thanks
Matt

 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Cartwright wrote:I understand that introducing a new variable and suppress warnings on its creation will eliminate the need to supress warnings on the entire method.

Correct.

Thing is that I don't know what the garbage collector makes out of that as this method will be used heavily with huge amount of data.

It won't make any difference. That variable goes out of scope right after this method, together with result. After this method has returned the only reference to the newly created array is the one you use in the calling method:


I know thta theoretically the GC will clean out the object once it has gone out of scope, but in real life and under heavy load it mostly is different.

An object never goes out of scope. Variables go out of scope. That may cause an object to lose its last active reference, thereby making the object eligible for garbage collection. As I said before, the two local variables will make no difference.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Rob,

and excuse my sloppy wording.

Should have been more precise.

Matt
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and here is what I make out of this discussion:


Thanks y'all
Matt
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic