Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help2200


This forum is provided to promote discussion amongst students enrolled in Data Structures and Algorithms (CITS2200).
 
Options:
RSS cloud
Jump to:

Unchecked cast from object to E

4 of 251 articles shown, currently no other people reading this forum.
photo
From: ANONYMOUS
Date: Tue 24th Mar, 6:11pm
Actions: 
        Login-to-reply
For Dequeue lab:

Using the Dequeue interface in CITS2200:

e.g peekRight() returns an E

Stored in Object[], when I get the item I cast the object to E

ie. return (E)queue[1] 

However I get a Unchecked cast from object to E warning. Google suggests suppressing 
it, but it doesnt see like the correct way to do it. The error shows up on the 
cssumbit near the top, even though its full marks.

Unchecked cast from object to E

photo
From: Xi R.
Date: Tue 24th Mar, 9:16pm
Actions: 
        Login-to-reply
Hey dude, how did u implement <E> object at the start of code? Like import CITS2200.Deque<E>? 

Unchecked cast from object to E

photo
From: ANONYMOUS
Date: Wed 1st Apr, 10:20am
Actions: 
        Login-to-reply
To be clear, an unchecked cast is just a warning, not an error. Your code compiles and 
runs fine with it, just it is upset with you because Java can't automatically know 
that that cast is a safe thing to. So feel free to suppress the warning, as you are 
effectively just telling the compiler "I know this is safe".

Unchecked cast from object to E

photo
From: ANONYMOUS
Date: Wed 1st Apr, 10:25am
Actions: 
        Login-to-reply
"Xi Rao"                               wrote:

> Hey dude, how did u implement <E> object at the start of code? Like import CITS2200.Deque<E>?

You need simply import CITS2200.Deque, which is an interface that can take a generic parameter

You then want to declare your class:

public class DequeCyclic<E> implements Deque<E> ...

This specifies that your class takes a generic parameter, E, and passess that generic parameter 
into the interface it implements.

Do note that the use of generics is optional and you can instead just use Object. If you do not 
provide a generic parameter, it defaults to Object. Hence Deque<Object> and Deque are the exact 
same thing.

The auto-tester tests your code as just DequeCyclic, so if you implement it using generics it 
will simply test DequeCyclic<Object>, so you can get away with just implementing object 
support.
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  8:27am May 24 2020