Solved

warning: [unchecked] unchecked cast

Posted on 2013-11-14
8
509 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 27

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 27

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 27

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 27

Expert Comment

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

Doug
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 57
jsp login check 12 43
hashmap order 17 37
What browser will run Java? 7 72
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 covers a step-by-step guide to install VisualVM launcher in eclipse.

821 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