Results 1 to 10 of 10
  1. #1
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Type safety Unchecked cast

    Hey people, can someone explain to me why I am getting this warning Type safety Unchecked cast from Comparable<E> to E and why I have to typecast in the first place.

    Java Code:
    public class ReferenceBasedList<E> implements ListInterface<E> {
    	private Node<E> head;
    	private Node<E> tail;
    	private static final int NOT_FOUND = -1;
    	int numItems;
    
    .....
    
    	 public void addInNaturalOrder(Comparable<E> dataItem) {
            if (isEmpty()) {
                insert((E)dataItem);
            } else {
                for (int i = 1; i <= size(); i++) {
                    if (dataItem.compareTo(get(i)) <= 0) {
                        add(i, (E)dataItem);
                        break;
                    }
                    if (i == size()) {
                        append((E)dataItem);
                        break;
                    }
                }
            }
        }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Type safety Unchecked cast

    Please copy the full text of the error message and paste it here. It has important info about the error.
    Don't copy parts of it, post the whole text.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Type safety Unchecked cast

    Type safety Unchecked cast from Comparable<E> to E I wrote it in the OP, it's not an error, but a warning from the IDE and it's on lines 11,15,19 .

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Type safety Unchecked cast

    Sorry, I don't know where those lines are. Can you find and post them?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Type safety Unchecked cast

    Quote Originally Posted by Norm View Post
    Sorry, I don't know where those lines are. Can you find and post them?
    The code is posted in the OP

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Type safety Unchecked cast

    Can you compile the code with the javac compiler to get an error message that clearly shows the error.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Type safety Unchecked cast

    Well, what do the methods insert, append and add look like?
    What parameters are they expecting?
    If it's an E then that's why you have to typecast.
    An E is not (necessarily) a Comparable<E>.
    Indeed, since that method needs a Comparable<E> then you should have defined the generic as (I think, my generics is rubbish) "<E extends Comparable<E>>" and not simply as "<E>".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Type safety Unchecked cast

    Yes, append and add take E

    I tried your suggestion but nothing changed. Can a problem occur with the current state?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: Type safety Unchecked cast

    Comparable<E> dataItem defines dataItem as a Comparable<E> object not as an E
    What if the arg is changed to E vs Comparable<E>?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default Re: Type safety Unchecked cast

    Yes, my bad I screwed up, it was correct. Thank you.

Similar Threads

  1. Replies: 2
    Last Post: 12-21-2012, 04:32 AM
  2. java enum : type safety
    By rsai in forum New To Java
    Replies: 2
    Last Post: 07-05-2012, 03:45 AM
  3. Menu Type cast Exception
    By arulmozs in forum AWT / Swing
    Replies: 8
    Last Post: 11-02-2009, 10:34 PM
  4. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  5. Help determing NamingEnumeration type/cast
    By BlankMan in forum Eclipse
    Replies: 1
    Last Post: 09-05-2008, 07:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •