Solved

warning: [unchecked] unchecked cast

Posted on 2013-11-14
8
490 Views
Last Modified: 2013-11-14
Hi,

I get this compile error when I try to compile my class with -Xlint:unchecked

warning: [unchecked] unchecked cast
		E[] newData = (E[]) new Object[capacity];
		                    ^
  required: E[]
  found:    Object[]
  where E is a type-variable:
    E extends Object declared in class ArrayList

Open in new window

If I compile without -Xlint, I get a warning, but no error, and the program works.

But it makes me nervous. Is there some way to 'fix' this to make -Xlint happy?

Thanks.
0
Comment
Question by:Kyle Hamilton
  • 4
  • 4
8 Comments
 
LVL 26

Expert Comment

by:dpearson
ID: 39648768
Have you tried:

E[] newData = new E[capacity] ;

Doug
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39648792
so, putting this in front of the constructor was supposed to eliminate this message, but it had no effect on the message I'm getting in the console:

@SuppressWarnings("unchecked")

Open in new window


I still get this message:

Note: ArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Open in new window


oh well, apparently this is not a big deal. If anyone has any input, I would appreciate it.

Thanks.
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39648801
Hi Doug,

according to the book,
E[] newData = new E[capacity] ;

Open in new window

would give ma an "invalid generic type" and won't compile.
0
 
LVL 26

Expert Comment

by:dpearson
ID: 39648851
Ah - didn't know it was generic.

That's certainly harder.  If you have access to the class itself you can do:

E createObject(Class<E> clazz)
{
      return clazz.newInstance();
}

To create a typesafe instance of the class.  (If we can get that working, then need to figure out how to extend it to the whole array).

But to do that you need to pass in E.class at the time of the call to the generic method/class - can't look it up later on inside the method because E will have had its type erased.

Is that an option?

Doug
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39648934
Ok, so I have this in my class:

private E createObject(Class<E> clazz) throws Exception{
		return clazz.newInstance();
	}

Open in new window


and now this:

E newData = new E[capacity];

Open in new window


now the error is:

error: generic array creation
		E newData = new E[capacity];

Open in new window


Would it be helpful if I posted the whole class code?
0
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 39648973
OK - it looks like the one you really want is
java.lang.reflect.Array.newInstance()

which allows you to create a new array of objects of a specific type, like E.class.

More docs on it here:
http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Array.html#newInstance(java.lang.Class, int)

And a bit of tutorial on the whole thing here:
http://docs.oracle.com/javase/tutorial/reflect/special/arrayInstance.html

Although I'm not sure this will ultimately clear the warning - as it seems
java.lang.reflect.Array.newInstance()
returns type Object
instead of type E[]
...

which looks wrong to me :)

Doug
0
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 39649010
Thank you. much appreciated.

I better get outta this rabbit hole, for now.

Once I pass Level 1, I'll revisit this :) I think for now, I'm gonna live with the warning.

Cheers,
Koza
0
 
LVL 26

Expert Comment

by:dpearson
ID: 39649168
Haha - fair enough :)

Doug
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now