Results 1 to 11 of 11
  1. #1
    alexbryan_08 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default cannot find symbol for "list.addFirst"

    Can somebody help me with this code..

    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;

    public class ListPerformanceDemo
    {
    final int SIZE = 100000;

    public ListPerformanceDemo()
    {
    System.out.println(Integer.MAX_VALUE + "\n\n");

    ArrayList<String> arrayL = new ArrayList<String>();
    System.out.println("time for ArrayList = " + timeAddFrontofList(arrayL));

    LinkedList<String> linkedL = new LinkedList<String>();
    System.out.println("time for LinkedList = " + timeAddFrontofList(linkedL));
    }

    //Add object to the front of the list using addFirst


    long timeAddFrontofList(List list)
    {
    long start = System.currentTimeMillis();

    for (int i = 0; i < SIZE; i++)
    {
    String s = new String(i + " ");
    // add object to the front of the list
    list.addFirst(s);
    }

    return System.currentTimeMillis() - start;
    }

    public static void main(String args[])
    {
    new ListPerformanceDemo();
    }
    }//end class


    apparently this code will work if i change it to list.add(0,s), but i want to use list.addFirst(s)..
    what should i change??
    :confused::confused:

  2. #2
    alexbryan_08 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    owh..
    and i actually want to add new object on the at the front of the list..

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

    Default

    What is this addFirst method? Where in the API have you seen this? You know that you can't just make up a method for an existing class and expect mathemagically that it will work.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    addFirst is a method in LinkedList. You are trying to call it on an ArrayList which is not a LinkedList. Better have the API specs open as you are coding.

  5. #5
    alexbryan_08 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    What is this addFirst method? Where in the API have you seen this? You know that you can't just make up a method for an existing class and expect mathemagically that it will work.
    DataStructures
    Class LinkedList

    java.lang.Object
    |
    +--DataStructures.LinkedList
    public class LinkedList
    extends java.lang.Object
    A simple implementation of singly linked lists.

    Lists are created empty, for example:

    LinkedList list = new LinkedList();

    after which items can be inserted at the front of the list by
    list.addFirst(item)

    -retrieve from website www[dot]informatics[dot]susx[dot]ac[dot]uk/courses/dats/DataStructures/docs/DataStructures/LinkedList

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

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

    Default

    Quote Originally Posted by r035198x View Post
    addFirst is a method in LinkedList. You are trying to call it on an ArrayList which is not a LinkedList. Better have the API specs open as you are coding.
    Arg, so it is. He's calling it on the List interface actually, and that is the API that I looked up. My bad. Please ignore what I stated above!

  8. #8
    alexbryan_08 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    addFirst is a method in LinkedList. You are trying to call it on an ArrayList which is not a LinkedList. Better have the API specs open as you are coding.
    im sorry..
    i dont understand..
    im pretty new on this..
    just learned it this sem..
    wat u mean wit specs open?
    :confused::confused:

  9. #9
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    6

    Default

    You have this method:

    Java Code:
    long timeAddFrontofList(List list)
    {
    ...
    }
    It is saying you have a variable named 'list' of class 'List'. The class List does not have a method 'addFirst' only the class LinkedList does.

    Specs:

    List
    LinkedList
    ArrayList
    Last edited by mrmatt1111; 08-26-2009 at 12:14 AM.
    My Hobby Project: LegacyClone

  10. #10
    alexbryan_08 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    owh..
    no wonder..
    change List -> LinkedList..

    problem solved..
    thanks everyone..
    :):)

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by alexbryan_08 View Post
    owh..
    no wonder..
    change List -> LinkedList..

    problem solved..
    thanks everyone..
    :):)
    By specs I meant API specs. Here is a sample. Programming without those is like shooting in the dark. You could even shoot your own foot.

Similar Threads

  1. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  2. Reading strings with like "degree symbol" - HOW?
    By RR_QQ in forum New To Java
    Replies: 13
    Last Post: 02-18-2009, 02:16 AM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. "Cannont find symbol Constructor" error
    By Welsh in forum New To Java
    Replies: 7
    Last Post: 01-25-2008, 12:12 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
  •