Solved

warning: [unchecked] unchecked cast

Posted on 2013-11-14
8
524 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 28

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 28

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
 
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 28

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 28

Expert Comment

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

Doug
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

628 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