aspose file tools*
The moose likes Java in General and the fly likes merging multiple arrays, unchecked warnings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "merging multiple arrays, unchecked warnings" Watch "merging multiple arrays, unchecked warnings" New topic
Author

merging multiple arrays, unchecked warnings

Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

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

Joined: Apr 04, 2009
Posts: 93
Actually
Array.newInstance
returns Object Array, so it is unknown what the type is.


Brainbench Java 2.0 Fundamentals, J2EE 1.4
http://www.brainbench.com/transcript.jsp?pid=8192792
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

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

Joined: Sep 29, 2008
Posts: 12617

Welcome to the world of Java generics.

Maybe start here.
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

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

Joined: Oct 27, 2005
Posts: 19670
    
  18

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

Joined: Aug 25, 2008
Posts: 149

Thanks again Rob,

and excuse my sloppy wording.

Should have been more precise.

Matt
Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149

and here is what I make out of this discussion:


Thanks y'all
Matt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: merging multiple arrays, unchecked warnings