Results 1 to 2 of 2
  1. #1
    wlc
    wlc is offline Member
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Default Need help using the Linked List class

    I'm trying to insert entries in and get entries from a linked list Java LinkedList class. I want to create a linked list of arrays, but I'm starting with a linked list of integers to keep thinks simple. I can use <list>.add() to add an entry to the linked list but when I use <list>.get(<index>) to get the integer, the compiler gives the message


    lltest.java:16: incompatible types
    found : java.lang.Object
    required: int
    myInt = list.get(0);


    I notice that

    System.out.println("list " + list.get(0));

    Will actually cause the integer to be displayed

    Here is the code that I'm working with

    import java.util.LinkedList;



    public class lltest {

    public static void main(String[] args) {

    int myInt = 5;


    LinkedList list = new LinkedList();
    list.add(myInt);
    myInt = 3;
    System.out.println("list " + myInt);
    myInt = list.get(0);
    System.out.println("list " + myInt);

    }
    }


    The full text when compiling is

    lltest.java:13: warning: [unchecked] unchecked call to add(E) as a member of the
    raw type java.util.LinkedList
    list.add(myInt);
    ^
    lltest.java:16: incompatible types
    found : java.lang.Object
    required: int
    myInt = list.get(0);
    ^
    1 error
    1 warning


    Can anyone help me understand how to add entries to and get entries from a LinkedList?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your LinkedList is not a generic LinkedList and has no knowledge of what type of object it holds, only that it holds an object. When you extract the object by calling get(), all Java knows is that an Object has been returned, not an Integer object.

    Your choices are to either cast the returned object as an Integer, and then call intValue() on the Integer object to get the int or else use a generic LinkedList<Integer>. This latter approach will allow the LinkedList to know that it holds Integer and only Integer objects and will only return Integer objects.

Similar Threads

  1. Linked list
    By rosh72851 in forum New To Java
    Replies: 1
    Last Post: 02-05-2009, 08:21 AM
  2. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 09:53 PM
  3. Linked List help
    By neobie in forum New To Java
    Replies: 8
    Last Post: 12-22-2007, 04:15 AM
  4. Linked List
    By rnavarro9 in forum New To Java
    Replies: 0
    Last Post: 11-29-2007, 04:42 AM
  5. Help with linked list
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:29 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
  •